我有一个包含两个不同表格的数据库。
**call_code** activity
call_id activity_id
maintCall_plan activity_desc
maintCall_unplanned contact_person
creditCalls day
newBussCalls activity_date
phoneCalls revenue
time
我在调试代码时遇到问题。问题是insert into
语句中的语法错误,并突出显示conConnection.Execute
。这是我的代码片段:
Private Sub Command1_Click()
Dim conConnection As ADODB.Connection
Dim cmdCommand As New ADODB.Command
Dim strSql As String
Set conConnection = New ADODB.Connection
conConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
App.Path & "\" & "db_weekActRep.mdb;Mode=Read|Write"
conConnection.CursorLocation = adUseClient
conConnection.Open
sqlStr = "INSERT INTO activity(activity_desc, contact_person, day, activity_date, revenue, time) VALUES ("
sqlStr = sqlStr & "'" & txtAct8am.Text & "',"
sqlStr = sqlStr & "'" & txtComp8am.Text & "',"
sqlStr = sqlStr & "'" & Label31.Caption & "',"
sqlStr = sqlStr & "'" & Label20.Caption & "',"
sqlStr = sqlStr & "'" & txtRev8am.Text & "',"
sqlStr = sqlStr & "'" & Label9.Caption & "')"
conConnection.Execute sqlStr
Select Case Combo1.ListIndex
Case 0
sqlStr = "INSERT INTO call_code(maintCall_plan) VALUES ("
sqlStr = sqlStr & "'" & "1" & "')"
conConnection.Execute sqlStr
Case 1
sqlStr = "INSERT INTO call_code(maintCall_unplanned) VALUES ("
sqlStr = sqlStr & "'" & "2" & "')"
conConnection.Execute sqlStr
Case 2
sqlStr = "INSERT INTO call_code(creditCalls) VALUES ("
sqlStr = sqlStr & "'" & "3" & "')"
conConnection.Execute sqlStr
Case 3
sqlStr = "INSERT INTO call_code(newBussCalls) VALUES ("
sqlStr = sqlStr & "'" & "4" & "')"
conConnection.Execute sqlStr
Case 4
sqlStr = "INSERT INTO call_code(phoneCalls) VALUES ("
sqlStr = sqlStr & "'" & "5" & "')"
conConnection.Execute sqlStr
End Select
End Sub
另外,我想知道是否可以使用一个点击事件或按钮将数据插入两个不同的表中?如果是的话,我做得对吗?
顺便说一句,我没有为活动表包含activity_id
,因为它是自动递增的。与call_id
表的call_code
相同。任何帮助将不胜感激。
答案 0 :(得分:0)
您的第一个INSERT
声明以错误的方式声明:
您有两个字段activity_desc, contact_person
但是您尝试插入两个以上的字段(六个用于精度)。所以你必须添加其他4个缺少的字段
sqlStr = "INSERT INTO activity(activity_desc, contact_person) VALUES ("
sqlStr = sqlStr & "'" & txtAct8am.Text & "',"
sqlStr = sqlStr & "'" & txtComp8am.Text & "',"
sqlStr = sqlStr & "'" & Label31.Caption & "',"
sqlStr = sqlStr & "'" & Label20.Caption & "',"
sqlStr = sqlStr & "'" & txtRev8am.Text & "',"
sqlStr = sqlStr & "'" & Label9.Caption & "')"
在INSERT INTO语句中添加4个缺少的字段。
两秒INSERT
(在CASE
语句中)不填充主键。如果你使用自动增量字段就可以了,否则它是错误的,因为主键不能为NULL
答案 1 :(得分:0)
首先,在INSERT INTO活动(activity_desc,contact_person)中,您有两列但只有6个值。放置2个值或添加4列。 即。 INSERT INTO活动(activity_desc,contact_person,day,activity_date,revenue,time)VALUES如果这些是您要添加的列或 sqlStr =" INSERT INTO活动(activity_desc,contact_person)VALUES(" sqlStr = sqlStr& "'" &安培; txtAct8am.Text& "'" sqlStr = sqlStr& "'" &安培; txtComp8am.Text& "'&#34 ;;
答案 2 :(得分:0)
我可以在这里看到三个潜在的问题:
您正在传递看起来像日期字段的字符串。您可能需要格式化该日期,以便MS-Access识别它,例如进入" YYYYMMDD"或" DD / MM / YYYY"格式。我无法记住Access是如何约会的,但我似乎记得它可能很挑剔。
您的文本字段可能包含单引号,因此您可能需要使用2 x单引号替换单引号,即" can' t"成为"可以'"
您还将看似数字的数字作为带引号的字符串传递。如果它没有解析为数字,那么这将引发错误。
调试启动程序,然后在执行语句之前将其分解。检查sqlStr的内容,然后将其复制/粘贴到您的数据库中。当你直接从MS-Access执行INSERT时,它可能会给你一个更有用的错误吗?
答案 3 :(得分:0)
我现在知道了......我在某些领域错过了一个支架。这是代码段..
sqlStr = "INSERT INTO activity(activity_desc, contact_person, revenue, [day], activity_date, [time]) VALUES ("
sqlStr = sqlStr & "'" & txtAct8am.Text & "',"
sqlStr = sqlStr & "'" & txtComp8am.Text & "',"
sqlStr = sqlStr & "'" & txtRev8am.Text & "',"
sqlStr = sqlStr & "'" & Label31.Caption & "',"
sqlStr = sqlStr & "'" & Label20.Caption & "',"
sqlStr = sqlStr & "'" & Label9.Caption & "')"
conConnection.Execute sqlStr
我只需要将日期和时间字段括在括号中..感谢您的回复我真的很感激..我的程序现在正在运行。再次感谢