简体   繁体   中英

MS Access: Object required Error with SQL statement

I understand what is the meaning behind this error, but I simply dont know why is he underling my line of code, that is supposed to cause this error.

EXPLANATION: What I am trying to achieve is, when you select a Company from CompanyCombo box , it will automaticly "filter" the ContactCombo box to those contacts that are from above mentioned company. Companies and Contacts are two different tables which are connected through Query called Companies Query (the CompanyNr [this is also UniqueKey ] from Companies is connected with CompanyNr from Contacts).

PART OF CODE:

Private Sub CompanyCombo_Change()
    Dim sql1 As String
    ' The below code causes the Objecr required error
    sql1 = "SELECT [Contacts].[ID], [Contacts].[Contact] FROM Contacts " & _
           "WHERE [Contacts].[CompanyNr] = " & CompanyNr.Value & " ORDER BY [Contact]; "

    ContactNrCombo.RowSource = sql1
    ContactNrCombo.Requery
End Sub

I have a ComboBox for Companies and Contacts, and I want to write next to them the appropriate number (CompanyNr for Companies and ContactNr for Contacts)

They are connected with query.

I am quite new to the VBA, so maybe I dont see something here what could be causing the error.

TABLES for CONTACTS and COMPANIES: Contact Table has: ID, CompanyNr, Contact Company Table has: CompanyNr, Company

CompanyNr are connnected via Relationships.

I really dont know what could be going wrong here. I created the ComboBox with the wizard that pops up when creating.

Please, anyone, help me out.

Just a try, because the question does not provide enough info at the moment:
Shouldn't the WHERE line refer to CompanyCombo instead of CompanyNr ?

"WHERE [Contacts].[CompanyNr] = " & CompanyCombo.Value & " ORDER BY [Contact]; "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM