VBA 运行-时间错误3075
VBA Run-time error 3075
我正在尝试在 VBA 中编写一个 SQL 查询,但它不起作用。不知道为什么,它是一个简单的 SELECT 查询。下面是我的 SQL 查询:
strSQLPharmContact = "SELECT TOP 1 tbl_Contacts.ID,
tbl_Contacts.idSite, tbl_Contacts.role, tbl_Contacts.name,
tbl_Contacts.email, tbl_Contacts.phone, tbl_Contacts.involvement,
tbl_Contacts.Taken" _ & "FROM tbl_Contacts " _
& "WHERE (((tbl_Contacts.role)= 'Pharmacist') AND
((tbl_Contacts.involvement)=True) AND ((tbl_Contacts.Taken)=False)); "
伙计们干杯
错误很可能是因为您的查询中存在间距问题。您需要一个 space 如下所示
& " FROM tbl_Contacts " _
^---- Here
否则,您的查询字符串看起来像
SELECT TOP 1 tbl_Contacts.ID, tbl_Contacts.idSite,
tbl_Contacts.role, tbl_Contacts.name,
tbl_Contacts.email, tbl_Contacts.phone,
tbl_Contacts.involvement, tbl_Contacts.TakenFROM tbl_Contacts
^-- ERROR Here
为什么这么多括号?
当使用 VBA 访问数据库时,我总是喜欢构造我的 SQL 查询,这样它看起来就像我将其输入到 SQL 查询工具中一样。
运行 并查看调试 window (ctrl-g)
更简洁,因此更容易修改和解决问题:
Sub sql()
strSQLPharmContact = "SELECT TOP 1 tbl_Contacts.ID,"
strSQLPharmContact = strSQLPharmContact & vbLf & " tbl_Contacts.idSite,"
strSQLPharmContact = strSQLPharmContact & vbLf & " tbl_Contacts.role,"
strSQLPharmContact = strSQLPharmContact & vbLf & " tbl_Contacts.name,"
strSQLPharmContact = strSQLPharmContact & vbLf & " tbl_Contacts.email,"
strSQLPharmContact = strSQLPharmContact & vbLf & " tbl_Contacts.phone,"
strSQLPharmContact = strSQLPharmContact & vbLf & " tbl_Contacts.involvement,"
strSQLPharmContact = strSQLPharmContact & vbLf & " tbl_Contacts.Taken"
strSQLPharmContact = strSQLPharmContact & vbLf & "FROM tbl_Contacts "
strSQLPharmContact = strSQLPharmContact & vbLf & "WHERE tbl_Contacts.role= 'Pharmacist'"
strSQLPharmContact = strSQLPharmContact & vbLf & "AND tbl_Contacts.involvement = True"
strSQLPharmContact = strSQLPharmContact & vbLf & "AND tbl_Contacts.Taken = False"
Debug.Print strSQLPharmContact
End Sub
我正在尝试在 VBA 中编写一个 SQL 查询,但它不起作用。不知道为什么,它是一个简单的 SELECT 查询。下面是我的 SQL 查询:
strSQLPharmContact = "SELECT TOP 1 tbl_Contacts.ID,
tbl_Contacts.idSite, tbl_Contacts.role, tbl_Contacts.name,
tbl_Contacts.email, tbl_Contacts.phone, tbl_Contacts.involvement,
tbl_Contacts.Taken" _ & "FROM tbl_Contacts " _
& "WHERE (((tbl_Contacts.role)= 'Pharmacist') AND
((tbl_Contacts.involvement)=True) AND ((tbl_Contacts.Taken)=False)); "
伙计们干杯
错误很可能是因为您的查询中存在间距问题。您需要一个 space 如下所示
& " FROM tbl_Contacts " _
^---- Here
否则,您的查询字符串看起来像
SELECT TOP 1 tbl_Contacts.ID, tbl_Contacts.idSite,
tbl_Contacts.role, tbl_Contacts.name,
tbl_Contacts.email, tbl_Contacts.phone,
tbl_Contacts.involvement, tbl_Contacts.TakenFROM tbl_Contacts
^-- ERROR Here
为什么这么多括号?
当使用 VBA 访问数据库时,我总是喜欢构造我的 SQL 查询,这样它看起来就像我将其输入到 SQL 查询工具中一样。
运行 并查看调试 window (ctrl-g)
更简洁,因此更容易修改和解决问题:
Sub sql()
strSQLPharmContact = "SELECT TOP 1 tbl_Contacts.ID,"
strSQLPharmContact = strSQLPharmContact & vbLf & " tbl_Contacts.idSite,"
strSQLPharmContact = strSQLPharmContact & vbLf & " tbl_Contacts.role,"
strSQLPharmContact = strSQLPharmContact & vbLf & " tbl_Contacts.name,"
strSQLPharmContact = strSQLPharmContact & vbLf & " tbl_Contacts.email,"
strSQLPharmContact = strSQLPharmContact & vbLf & " tbl_Contacts.phone,"
strSQLPharmContact = strSQLPharmContact & vbLf & " tbl_Contacts.involvement,"
strSQLPharmContact = strSQLPharmContact & vbLf & " tbl_Contacts.Taken"
strSQLPharmContact = strSQLPharmContact & vbLf & "FROM tbl_Contacts "
strSQLPharmContact = strSQLPharmContact & vbLf & "WHERE tbl_Contacts.role= 'Pharmacist'"
strSQLPharmContact = strSQLPharmContact & vbLf & "AND tbl_Contacts.involvement = True"
strSQLPharmContact = strSQLPharmContact & vbLf & "AND tbl_Contacts.Taken = False"
Debug.Print strSQLPharmContact
End Sub