获取文本框的值作为Access中的字段名称

时间:2019-02-21 15:35:07

标签: excel vba ms-access

基本上,我希望将combobox1的值用作下面查询的字段名称。有人可以帮我吗?

Dim db As Database
Dim rs As DAO.Recordset

Set db = OpenDatabase("\\location\file.mdb")
Set rs = db.OpenRecordset("select * from customerinfo " _
  & "where '"& (combobox1.text) &"' likE '*" & (txtsearch) & "*';")

If rs.RecordCount = 0 Then
    MsgBox "No Item Found"
Else
    Do While Not rs.EOF = True
        listbox.AddItem

        On Error Resume Next
        listbox.List(listbox.ListCount - 1, 0) = rs("Fieldname").Value

        rs.MoveNext
    Loop
end if

1 个答案:

答案 0 :(得分:1)

您的查询有错误:

where '"& (combobox1.text) &"' likE 

这将创建不正确的where子句where 'fieldname' likE,该子句应为
where fieldname likeE。将查询更改为:

where "& (combobox1.text) &" likE 

注意:最好在变量中创建查询字符串。这样可以更轻松地发现任何错误。