我对vba访问有问题,因为我不知道语法。我的功能将从表" IC4_MMI_405"表中读取字段,从excel文件访问中正确导入:
Dim conn As New ADODB.Connection
Dim record As New ADODB.Recordset
Set conn = CurrentProject.Connection
Set record = New ADODB.Recordset
record.Open "SELECT * WHERE file = '" & sFile & "' And ccu_code = " & iCode, conn, adOpenKeyset, adLockOptimistic
使用此代码,一切都很好。如果我以这种方式更改代码:
Dim conn As New ADODB.Connection
Dim record As New ADODB.Recordset
Dim sSQL As String
Set conn = CurrentProject.Connection
Set record = New ADODB.Recordset
sSQL = "SELECT * FROM IC4_MMI_410 WHERE file = "
sSQL = sSQL & "" & sFile & ""
sSQL = sSQL & " And ccu_code = " & iCode
record.Open sSQL, conn, adOpenKeyset, adLockOptimistic
我将收到运行时间错误。Sfile是字符串,Icode是整数。当我以调试模式运行时,我可以看到SFILE和ICODE的值是预期的。我认为问题在我写SSQL的方式上存在。你能帮我吗?
这应该有效:
Dim conn As New ADODB.Connection
Dim record As New ADODB.Recordset
Dim sSQL As String
Set conn = CurrentProject.Connection
Set record = New ADODB.Recordset
sSQL = "SELECT * FROM IC4_MMI_410 WHERE file = "
sSQL = sSQL & "'" & sFile & "'"
sSQL = sSQL & " And ccu_code = " & iCode
record.Open sSQL, conn, adOpenKeyset, adLockOptimistic