简体   繁体   中英

SQL Command “Enter Parameter Value” With ComboBox Data

I currently have the following code

Private Sub dbSearch_Click()

Dim ManfactQuery As String
Dim ModelQuery As String
Dim strSQL As String

ManfactQuery = Me.cboManfact.Column(1)
ModelQuery = Me.cboModel.Column(1)

If ManfactQuery = Null Or ManfactQuery = Null Then
strSQL = "SELECT [Solutions].SolutionText FROM [Solutions] WHERE [Solutions].ModelSolution = " & ModelQuery & ""

    Else

        If ModelQuery = Null Or ModelQuery = "" Then
    strSQL = "SELECT [Solutions].SolutionText FROM [Solutions] WHERE [Solutions].ManufacturerSolution = " & ManfactQuery & ""

    Else

        strSQL = "SELECT [Solutions].SolutionText FROM [Solutions] WHERE [Solutions].ManufacturerSolution = " & ManfactQuery & " AND   [Solutions].ModelSolution = " & ModelQuery & ""
    End If

End If

Me.lstSolution.RowSource = strSQL

End Sub

Im trying to pass the text values of the combobox into the SQL statement and it works to and extent, but i get "Enter Parameter value" popup dialog box. If i then input the value of the comboboxes and click ok, it populates the listbox with the appropriate field, but I need to not have the dialog box if possible.

Any ideas please?

First of all you probably mean this:

If ManfactQuery = Null Or ManfactQuery = Null Then

to be this:

If ManfactQuery = Null Or ManfactQuery = "" Then

and it can be better represented like this:

If Nz(ManfactQuery)="" then

Now to the actual issue. Since ManfactQuery and ModelQuery are text values they need to be surrounded by single quotes in your SQL text like this:

strSQL = "SELECT [Solutions].SolutionText FROM [Solutions] WHERE [Solutions].ModelSolution = '" & ModelQuery & "'"

SOLVED IT. Thank you so much @Charlie and @SunKnight0

Code is as follows:

Private Sub dbSearch_Click()

Dim ManfactQuery As String
Dim ModelQuery As String
Dim strSQL As String

ManfactQuery = Me.cboManfact.Column(1)
ModelQuery = Me.cboModel.Column(1)

If Len(ModelQuery) = 0 Then

strSQL = "SELECT [Solutions].SolutionText FROM [Solutions] WHERE [Solutions].ModelSolution = '" & ModelQuery & "'"

    Else

If Len(ManfactQuery) = 0 Then

strSQL = "SELECT [Solutions].SolutionText FROM [Solutions] WHERE [Solutions].ManufacturerSolution = '" & ManfactQuery & "'"

    Else

strSQL = "SELECT [Solutions].SolutionText FROM [Solutions] WHERE [Solutions].ManufacturerSolution = '" & ManfactQuery & "' AND [Solutions].ModelSolution = '" & ModelQuery & "'"

    End If

End If

Me.lstSolution.RowSource = strSQL

End Sub

Thank you all so much, after 3 days of trying to figure this out, ive got it!

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