Access-VBA SQL Query Database and Update ListBox with results - sql

So I have do a lot of questioning around, looking online, and all over the web and I am more than likely being blind, but I'm out of ideas.
To break down my problem, i have the following:
Contained within form "Troubleshoot" is the following
2 comboboxes named "cboManfact" and "cboModel"
1 listbox named "lstSolutions"
1 button named "HomeReturn"
1 button named "db_Search"
Contained within table "Solutions" i have the following Fields
ID
ManufacturerSolution
ModelSolution
DateSolution
UserSolution
SolutionText
Now the complicated part.
"lstSolutions" needs to display "SolutionText" however there is a catch.
"cboManfact" and "cboModel" contain the list of manufacturers and model numbers stored in a seperate table which works perfectly.
what i need to have. Is "lstSolutions" display "SolutionText" only when "cboManfact" text and "cboModel" text match an entry in the solutions table.
So for instance:
cboManfact = Turbo
cboModel = 1600
On clicking the "db_Search" button, "lstSolutions" is populated with all results for "SoltionText" where "ManufacturerSolution" = "cboManfact" and "ModelSolution" = "cboModel"
The problem with the SQL query i currently have is that it populates the list box with every result from "Solutions" but upon clicking the "db_search" button which performs a requery, the list box empties so i dont think the SQL command makes sure that cbomanfact etc match.
Here is what i have at the moment:
lstSolutions recordSource:
SELECT [SolutionText] FROM [Solutions] WHERE solutions.ManufacturerSolution like forms![Troubleshoot]!cboManfact & "*" AND solutions.ModelSolution like forms![Troubleshoot]!cboModel & "*"
db_click code:
Private Sub dbSearch_Click()
me.lstSolutuions.requery
end sub
If anyone has any ideas, or has a better way of explaining this it would help. I have had help from LiamH on a seperate thread which has helped majorly but i created this in order to explain the issue clearer hopefully.
I understand to an extent what is going on now, but Im just not experienced enough to work around this myself and would like any ideas of what people would suggest.

Solved the problem using the following code on button click:
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 Nz(ManfactQuery) = "" Then
strSQL = "SELECT [Solutions].SolutionText FROM [Solutions] WHERE [Solutions].ModelSolution = '" & ModelQuery & "'"
Else
If Nz(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

Related

DoCmd.OpenForm with a Like in Access is not working for me

I'm pretty new at Access and I am trying to open a form with a like filter using the code
Private Sub AddCOB_Click()
Dim FilterLike As String
If Nz(JobSwitch, 0) = 0 Then
MsgBox ("Please Select a Job")
JobSwitch.SetFocus
Else
FilterLike = "[ID] = LIKE '" & jobNUMBER & "*'"
DoCmd.OpenForm "AddCOF", , , FilterLike
End If
End Sub
I keep getting Runtime error 3075
jobNUMBER is a public variable declared in a module and defined in this form, that seems to be working fine. This message is showing what I believe is the right syntax. I also checked it with a MsgBox. I cannot figure out what I am doing wrong. Can someone please show me the error of my ways?
= and LIKE are different comparison modes. Access expects you to use one or the other.
In your case, use [ID] LIKE
FilterLike = "[ID] LIKE '" & jobNUMBER & "*'"

MS Access - Main menu form with 2 text boxes used to open and filter a different form - getting a type mismatch (Run error 13)

I have a main menu where users type in a name of a station (text box) and the name of a cohort (text box) and it would open a new form based on these values. For example, if "New York" was entered for a station and "1b" was entered for cohort, then the form would filter to only show data that have both. However, I am getting a data mismatch error.
The fields text boxed at my main menu are called "detailed_s_station" and "detailed_s_cohort". The names of the respective fields in the form I want to filter these values are called "station" and "cohort".
I can get this to work if there are only one set of criteria (e.g., if I just search for the cohort or just search for the station), however something is going on with my AND here. Any help is appreciated to help me get rid of this data mismatch error.
Private Sub Command41_Click()
Dim stDocName22 As String
Dim stLinkCriteria22 As String
stDocName22 = "frm_scans"
stLinkCriteria22 = "[station] ='" & Me![detailed_s_station] & "'" And "[cohort] ='" & Me![detailed_s_cohort] & "'"
DoCmd.OpenForm stDocName22, , , stLinkCriteria22, acFormEdit, acWindowNormal
End Sub
The problem that you are having is how you are joining the string together. Instead try:
Private Sub Command41_Click()
Dim stDocName22 As String
Dim stLinkCriteria22 As String
stDocName22 = "frm_scans"
stLinkCriteria22 = "[station] ='" & Me![detailed_s_station] & "' And [cohort] ='" & Me![detailed_s_cohort] & "'"
DoCmd.OpenForm stDocName22, , , stLinkCriteria22, acFormEdit, acWindowNormal
End Sub
If you ever have problems like this in future, a quick Debug.Print strLinkCriteria22 would show you the contents of the string that is causing problems.
Regards,

Run-Time error 3075 Access Syntax Error for combobox Search in Access

I am learning about how to create a searchbox with combo box. I was learning with a video in youtube :
Access: How to Create Search Form Using Combo box Part 1
However, when I do my code it doesn't work. :/ I get the Run-Time error 3075 Access Syntax Error.
Private Sub cboVendorSearch_AfterUpdate()
Dim MyVendor As String
MyVendor = "Select * from Vendors where ([vend_name] = " & Me.cboVendorSearch & ")"
Me.Invoices_subform.Form.RecordSource = MyVendor
Me.Invoices_subform.Form.Requery
End Sub
Assuming vend_name is a text field, need apostrophe delimiters.
MyVendor = "SELECT * FROM Vendors WHERE [vend_name] = '" & Me.cboVendorSearch & "';"
Instead of setting RecordSource, an alternative is to set Filter property.
Me.Invoices_subform.Form.Filter = "[vend_name] = '" & Me.cboVendorSearch & "'"
Me.Invoices_subform.Form.FilterOn = True
Would probably be better to use vendor ID for search criteria. Explore multi-column combobox where the ID field is a hidden column but combobox uses the hidden column for its Value yet displays vend_name to user.

update table through form

This is the next attempt I am making at solving this problem. In my timeline is the prior attempt.
I have three tables BOAT, BERTHAGE, LOCATION. These tables are joined in the following way in a query called Boat_Move
BOAT(boat_ID) joins to BERTHAGE(boat)
LOCATION(Loc_ID) joins to BERTHAGE(location)
This query is viewed on the form as a subform.
When the form opens up I will highlight the vessel which needs to have its location moved/updated
Once highlighted in the subform I click the edit button
Private Sub cmd_Edit_Click()
If Not (Me.Current_Week_Boat_Move.Form.Recordset.EOF And Me.Current_Week_Boat_Move.Form.Recordset.BOF) Then
With Me.Current_Week_Boat_Move.Form.Recordset
Me.txt_Boat = .Fields("BOAT_NAME")
Me.txt_Cur_Loc = .Fields("LOCATION")
Me.txt_Cur_Flo = .Fields("FLOAT")
Me.txt_Cur_Doc = .Fields("DOCK")
Me.txt_Cur_Ori = .Fields("ORIENTATION")
Me.cmd_Add.Caption = "Update"
Me.cmd_Edit.Enabled = False
End With
End If
End Sub
This puts the appropriate information into the text boxes.
I've also did up the following with the idea that this would update the location(field) in the BERTHAGE(table)
Private Sub cmd_Add_Click()
Dim strSQL As String
strSQL = "INSERT INTO BERTHAGE(BOAT, LOCATION)" & _
" VALUES(" & Me.txt_Boat & ",'" & Me.txt_Cur_Loc & "');"
CurrentDb.Execute "UPDATE BERTHAGE " & " SET LOCATION=" & Me.txt_Cur_Loc & _
" WHERE LOCATION=" & Me.txt_Cur_Loc
Current_Week_Boat_Move.Form.Requery
cmd_Clear_Click
End Sub
I'm not sure if in this instance I should be creating a new table to make the update and then overwriting the older table. The issue I see here is the older table is at around 24k records so that doesn't seem like the best way forward.
So at this point I'm facing two problems, and I am unsure if solving either one will solve the actual problem of updating the BERTHAGE(location) field.
View of the from
Any help is appreciated.

Access 2016 - Multiple text fields - Search as you type not working

I have an Access 2016 Form with 5 text boxes labeled txtprod, txtpri, txtcnt, txtph, txtmfg. I also have a query for my table for the product name, price, count, phone and manufacturer fields.
In my forms 5 text boxes, I would like to be able to auto-search as you type and have my list auto-populate.
I followed these tutorials
https://www.youtube.com/watch?v=SJLQqwMOF08
https://www.youtube.com/watch?v=MwaRFjgwBW8
My form has a form load:
*Private Sub Form_Load()
Dim task As String
task = "SELECT * FROM pricingdata"
Me!details.RowSource = task
End Sub*
And my text box name has this event "on change"
*Private Sub txtprod_Change()
Dim task As String
task = "SELECT * FROM [pricingdata] WHERE ([Product Name] LIKE '" & Me.txtprod.Text & "');"
Me!details.RowSource = task
End Sub*
Search as I type works perfectly fine with just 1 text box. But when I add the following code to my Manufacturer text box event "on change" it doesn't work as intended.
*Private Sub txtmfg_Change()
Dim task As String
task = "SELECT * FROM [pricingdata] WHERE ([Manufacturer] LIKE '" & Me.txtmfg.Text & "');"
Me!details.RowSource = task
End Sub*
Now when I type in my Product name text box, it searched products just fine. When I start typing in my Manufacturers text box, it completely disregards anything I've put into the Product name text box and starts searching as I type only for text in the Manufacturers text box field.
How can I get this setup so all text in all 5 text box fields contribute to the search filter being applied to my list?
Something like this . . .
Private Sub ComboSelect_Change()
' You need to use String delimiters if you want to use a Text Field like:
' Me.Filter "[ATextFieldInRecordSource] = """ & Me.FilterComboBox & """"
' For a Numeric Field, use something like this:
' Me.Filter "[ANumericFieldInRecordSource] = " & Me.FilterComboBox
' Me.FilterOn = True
Me.[Customer_Query subform1].Form.Filter = "[Company_Name] Like '*" &
Replace(Me.ComboSelect.Text, "'", "''") & "*'"
Me.[Customer_Query subform1].Form.FilterOn = True
End Sub
Notice a few things:
The subform is named Customer_Query subform1’
The combobox is named ComboSelect’
Finally, the ‘like clause’ is used in combination with the wildcard character.
Like '*" & Replace(Me.ComboSelect.Text, "'", "''") & "*'"
When you type text into the combobox, the results in the subform are dynamically re-queried.