访问-Excel(VBA)-类型不匹配

时间:2019-03-06 04:13:25

标签: excel vba ms-access time-and-attendance

我们正尝试使用VBA将Access数据库中的数据加载到Excel。但是我们的代码无法正常工作。

SQL1 = "SELECT Name " & _
SQL1 = SQL1 & "FROM February" & _
SQL1 = SQL1 & "WHERE Gender = 'F'" & _
SQL1 = SQL1 & "ORDER BY Name"
Set rs1 = db.OpenRecordset(SQL1, dbOpenSnapshot)

 If rs1.RecordCount = 0 Then
    MsgBox "No data retrieved from database", vbInformation + vbOKOnly, "No Data"
     GoTo SubExit
 Else
     rs1.MoveLast
     recCount = rs1.RecordCount
     rs1.MoveFirst
End If
  xlSheet.Range("B28").CopyFromRecordset rs1

我们在行中收到“类型不匹配”错误:

Set rs1 = db.OpenRecordset(SQL1, dbOpenSnapshot)

任何人都可以帮助我们并向我们解释为什么会出现该错误吗?

1 个答案:

答案 0 :(得分:1)

SQL1 = "SELECT Name " & _
SQL1 = SQL1 & "FROM February" & _
SQL1 = SQL1 & "WHERE Gender = 'F'" & _
SQL1 = SQL1 & "ORDER BY Name"

应该是

SQL1 = " SELECT Name " & _
       " FROM February" & _
       " WHERE Gender = 'F'" & _
       " ORDER BY Name"

...或者不使用行继续符并使用SQL1 = SQL1 &,但是您只能使用其中的一个