Search all records in subform from main form - vba

I have a button that can search locations in all records in a table in the subform.
But it seems to show all records that have the [Location] in them instead of only records with the specific location entered in the textbox.
But once I've done my search, I can't seem revert the form to the original clear state, so that I can go back to searching other things.
Private Sub StartSearch2_Click()
Dim rs As Recordset
Dim strSQL As String
strSQL = "select * from [FormTable] where [Location]='" & Me.LocSearch & "'"
Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
If Not rs.BOF And Not rs.EOF Then
Set Me.Recordset = rs
Else
MsgBox "No record found", vbOKOnly + vbInformation, "Sorry"
Me.RecordSource = strOriginalSQL
End If
Me.LocSearch = Null
End Sub

Another approach is to not change the Record Source of your form and instead set the Filter property.
Set the Record Source to FormTable. You can do this in the form designer.
Then set the Filter with
Me.Filter = "Location='" & Me.LocSearch & "'"
Me.FilterOn = True
You can clear the filter with
Me.Filter = ""
Me.FilterOn = False
If you want to filter a subform, you can do this from the main form with
With Me!mysubform.Form
.Filter = "Location='" & Me.LocSearch & "'"
.FilterOn = True
End With
It is a good idea to escape any single quotes in the search string
Me.Filter = "Location='" & Replace(Me.LocSearch, "'", "''") & "'"

Related

Click on record from a form to go to associated record in same form

Looking for a way to click on a record in a form and then have it go to another record in the same form.
Example: There's a form that lists Products and associated records along with that product. One of the records is a "Replaced" field. Which will show what past Products it replaced.
I want to be able to click in the Replaced field and have it go to that Product record.
txtProduct: Iphone6
txtReplaced: Iphone5 (click on Iphone5) -> and then shows
txtProduct: Iphone5
txtReplaced: Iphone4
I looked into GoToRecord and FindRecord. Thinking the best way to go about it is to put vba code into the On Click event procedure using one of those for txtReplaced. Just not sure how to do that
EDIT to include attempt using RecordsetClone. It's now giving me the error that it can't find the record when I know it's there. Any insight on what I'm doing wrong?
Private Sub txtSupersedes_DblClick(Cancel As Integer)
Dim rs As Object
Set rs = Me.RecordsetClone
rs.FindFirst "[Model]='" & Me.txtSupersedes.Value & "'"
If rs.NoMatch Then
MsgBox "Sorry, could not find Model '" & txtSupersedes & "' ", vbOKOnly, vbInformation
Else
Me.Bookmark = rs.Bookmark
End If
rs.Close
End Sub
Private Sub txtSupersedes_DblClick(Cancel As Integer)
Dim rs As Object
Dim txtValue As String
txtValue = Me![txtSupersedes].Value
With Me.Parent.Form
Set rs = .RecordsetClone
rs.FindFirst "[Model]= '" & txtValue & "'"
If rs.NoMatch Then
MsgBox "Sorry, could not find Model '" & txtSupersedes & "' ", vbOKOnly, vbInformation
Else
.Bookmark = rs.Bookmark
End If
rs.Close
End With
End Sub

Checkbox Yes/No Change on Dropbox Change

I have a Combo Box (cbo1) that list available Items. When I select an item in cbo1, I would like it change a checkbox to True (or Yes).
cbo1 gets data from tblLOG where Box (checkbox) is NO Query
I've tried using
UPDATE tblLOG
Set Box = True
WHERE Serial = cboSerial
Actual Code.
Private Sub cbo1_Change()
Dim strSQL As String
Dim i As Integer
Dim Msg As String
Dim Assm As String
Assm = cbo1.Value
'Exit this sub if the combo box is cleared
Msg = "Make Update" & vbCr & vbCr
i = MsgBox(Msg, vbQuestion + vbYesNo, "UPDATE VALUE?")
If i = vbYes Then
strSQL = "UPDATE tblLOG " _
& "SET Box= True " _
& "WHERE Serial = Assm;"
CurrentDb.Execute strSQL, dbFailOnError
Response = acDataErrAdded
End If
End Sub
My Results are
Run-time error '3061': Too few parameters. Expected 1.
The reason for your error is because you are not evaluating your VBA variable Assm, but rather concatenating the string "Assm" to your SQL query.
strSQL = "UPDATE tblLOG " _
& "SET Box= True " _
& "WHERE Serial = Assm;"
If you were to Debug.Print the variable strSQL to the console, you would see the string:
"UPDATE tblLOG SET Box= True WHERE Serial = Assm;"
However, since Assm is not a string in the SQL query (i.e. it is not surrounded by single or double quotes), it is interpreted as a parameter whose value hasn't been supplied when the SQL query is subsequently executed.
To solve this, you could concatenate the evaluated value of the Assm variable, e.g.:
strSQL = "UPDATE tblLOG " _
& "SET Box= True " _
& "WHERE Serial = '" & Assm & "';"
This assumes that Serial is a text field - if this is not the case, remove the single quotes from the above.
Your entire code could be condensed somewhat to:
Private Sub cbo1_Change()
If MsgBox("Make Update", vbQuestion + vbYesNo, "UPDATE VALUE?") = vbYes Then
CurrentDb.Execute "update tbllog set box = true where serial = '" & cbo1 & "';", dbFailOnError
Response = acDataErrAdded
End If
End Sub
Though, this is still open to SQL injection, and so a better practice is to parameterise the query, e.g.:
With CurrentDb.CreateQueryDef("", "update tbllog t set t.box = true where t.serial = myserial;")
.Parameters!myserial = cbo1
.Execute
End With

Access Split column data w semi-colon into normalize table structure

I have a table, which was pulled out of some XML data. I'm trying to do a cross reference, so I can line out a plan for organizing the data. This 1 table has a list of variables. Fields of different data types, computations, as well as dialogs. One of the columns has options. If the data type of the variable is a dialog, its options has a list of variables, separated by a semi-colon.
So the main table has a structure like so:
For the dialog records I need to look through their options column and insert records into a normalized table. For each field, in that column, I want to add a record with that dialog name, and the ID of the row in that table (I added a PK to the table). For instance, in the dialog record, Options column, there is a field in there called BusinessName TE. I need to search this main table for the PK ID of the row that has a variable name of the same. I need to put that record's ID with the name of the dialog, and insert both into a new table I set up. This will create a cross reference for me, so I can know which variables are being used by which dialogs.
I appreciate any help anyone can give. I see stuff about using a split function, arrays and looping through to get each value, but the examples I'm finding are for strings, not a column in a table.
Thanks!
Edit: Adding in the VBA code I'm working with. I attached it to a button on a form, just so I could click to run it.
Private Sub RunParse_Click()
Dim db As DAO.Database
Dim rs As Recordset
Set db = CurrentDb()
Dim sqlStr, insertSQL, arrayVal As String
Dim TestArray As Variant
Dim Options As String
Dim Dialog As String
Dim FieldName As Long
Dim i As Integer
sqlStr = "SELECT [MASTER Fields].Options,[MASTER Fields].[Variable Name] FROM [MASTER Fields] WHERE ((([MASTER Fields].[Variable Type])='dialog'));"
Set rs = db.OpenRecordset(sqlStr)
rs.MoveLast
rs.MoveFirst
Do While Not rs.EOF
Options = rs.Fields(0)
Dialog = rs.Fields(1)
If InStr(Options, ";") Then
TestArray = Split(Options, ";")
For i = 0 To UBound(TestArray) - LBound(TestArray) + 1
If TestArray(i) <> "" Then
arrayVal = TestArray(i)
FieldName = DLookup("ID", "MASTER Fields", "[Variable Name] = " & "'" & arrayVal & "'")
insertSQL = "INSERT INTO FieldTemplatesUse(FID, TemplateAK) " _
& "VALUES(""" & FieldName & """, """ & Dialog & """)"
DoCmd.RunSQL (insertSQL)
End If
Next i
End If
rs.MoveNext
Loop
End Sub
right now on the line that says
If TestArray(i) <> "" Then
creates an error ""
If anyone can help, I'd really appreciate it!
Another Edit:
Parfait figured out my issue. I'm posting the final code I am using, in case it helps someone else! p.s. I added a condition to check if the dlookup is successful, and trap failures in a failures table. That way I can check those out afterward.
Private Sub RunParse_Click()
Dim db As DAO.Database
Dim rs As Recordset
Set db = CurrentDb()
Dim sqlStr, insertSQL, arrayVal As String
Dim TestArray As Variant
Dim Options As String
Dim Dialog As String
Dim FieldName As Long
Dim i As Integer
sqlStr = "SELECT [Master Fields].Options,[Master Fields].[Variable Name] FROM [Master Fields] WHERE ((([Master Fields].[Variable Type])='dialog'));"
Set rs = db.OpenRecordset(sqlStr)
rs.MoveLast
rs.MoveFirst
Do While Not rs.EOF
Options = rs.Fields(0)
Dialog = rs.Fields(1)
If InStr(Options, ";") Then
TestArray = Split(Options, ";")
For i = 0 To UBound(TestArray) - LBound(TestArray)
If TestArray(i) <> "" Then
arrayVal = TestArray(i)
If Not (IsNull(DLookup("ID", "Master Fields", "[Variable Name] = " & "'" & arrayVal & "'"))) Then
FieldName = DLookup("ID", "Master Fields", "[Variable Name] = " & "'" & arrayVal & "'")
insertSQL = "INSERT INTO FieldTemplatesUse(FID, TemplateAK) " _
& "VALUES(""" & FieldName & """, """ & Dialog & """)"
DoCmd.RunSQL (insertSQL)
'MsgBox "Adding ID = " & FieldName & "for Dialog: " & Dialog & "Now"
Else
insertSQL = "INSERT INTO tblFieldsNotFound(Dialog, FieldNotFound) " _
& "VALUES(""" & Dialog & """, """ & arrayVal & """)"
DoCmd.RunSQL (insertSQL)
End If
End If
Next i
End If
rs.MoveNext
Loop
MsgBox "All Done!"
End Sub

Search using multiple criteria

I have two textboxes I want to search a table. I am putting the search through a query which I believe to be the issue. the code should take the criteria from both textboxes and search one table. after that the rest of the textboxes will fill with the recordset the is closest to the search.
I get a error at RstRecSet.MoveLast I get a compile error that says no current record. I do have a record like what I type into the box its not picking it up though
When I add in the exact name of the record it finds it no problem. It's almost as if it's treating the like operator as if it were a =.
Here is my code:
Private Sub Command514_Click()
'DoCmd.Close
'DoCmd.OpenForm "frmContacts", acNormal
Set RstRecSet = Nothing
Set db = CurrentDb
Dim searchNum As String
Dim searchName As String
searchNum = txtGroupNr
searchName = txtGroupName
On Error Resume Next
If IsNull(txtGroupNr) Or txtGroupNr = "" Then
Me.txtGroupName.BackColor = vbRed
Forms!frmGroupHeader!txtGroupNr.SetFocus
Else
'strSearchICN = txtGroupNr
Set db = CurrentDb
Me.txtGroupName.BackColor = vbWhite
Set RstRecSet = db.OpenRecordset("Select * from tblGroupHeader Where GroupNum Like '" & searchNum & "' And GroupName Like '" & searchName & "';", dbOpenDynaset)
RstRecSet.MoveLast
intMaxCount = RstRecSet.RecordCount
RstRecSet.MoveFirst
' Exit Sub
End If
If RstRecSet.EOF Then
Me.txtGroupName.BackColor = vbRed
Forms!frmGroupHeader!txtGroupNr.SetFocus
Else
Call DisplayFields
End If
End Sub
Here is the solution:
Private Sub Command514_Click()
'DoCmd.Close
'DoCmd.OpenForm "frmContacts", acNormal
Set RstRecSet = Nothing
Set db = CurrentDb
Dim searchGroup As String
Dim searchName As String
If IsNull(txtgroupSearch) Or txtgroupSearch = "" Then
Me.txtGroupName.BackColor = vbRed
Forms!frmGroupHeader!txtGroupNr.SetFocus
Else
'searchNum = txtGroupNr
searchGroup = txtgroupSearch
Set db = CurrentDb
Me.txtGroupName.BackColor = vbWhite
Set RstRecSet = db.OpenRecordset("Select * from tblGroupHeader Where groupName like '*" & searchGroup & "*' or groupNum like '*" & searchGroup & "*';", dbOpenDynaset)
If RstRecSet.EOF And RstRecSet.BOF Then
MsgBox ("NO RECORDS!")
Exit Sub
End If
RstRecSet.MoveLast
intMaxCount = RstRecSet.RecordCount
RstRecSet.MoveFirst
' Exit Sub
End If
If RstRecSet.EOF Then
Me.txtGroupName.BackColor = vbRed
Forms!frmGroupHeader!txtGroupNr.SetFocus
Else
Call DisplayFields
End If
End Sub
I believe the problem is in your SQL. The error suggests that you have no records. Before calling .MoveLast on the recordset you could put in a quick test with:
If RstRecSet.EOF and RstRecSet.BOF Then
msgbox("NO RECORDS!")
exit sub
End if
Back to the SQL. You wrote "Select * from tblGroupHeader Where GroupNum and GroupName Like '" & searchNum & GroupName & "';" which, if you chose "1" as the GroupNum and "Physics" as the Groupname, would come out to something like Select * from tblGroupHeader Where GroupNum and GroupName Like '1Physics'; Which makes no sense
Instead: "Select * from tblGroupHeader Where GroupNum Like '" & searchNum & "' and GroupName Like '" & GroupName & "';" Which would look like Select * from tblGroupHeader Where GroupNum Like '1' and GroupName Like 'Physics';
I've found it helpful to put my SQL into a variable first and then write that variable to the Immediates window with a Debug.Print SQL so that I can just copy and paste back into my database to make sure that everything makes sense.

Access 2007 VBA & SQL - Update a Subform pointed at a dynamically created query

Abstract:
I'm using VB to recreate a query each time a user selects one of 3 options from a drop down menu, which appends the WHERE clause If they've selected anything from the combo boxes. I then am attempting to get the information displayed on the form to refresh thereby filtering what is displayed in the table based on user input.
1) Dynamically created query using VB.
Private Sub BuildQuery()
' This sub routine will redefine the subQryAllJobsQuery based on input from
' the user on the Management tab.
Dim strQryName As String
Dim strSql As String ' Main SQL SELECT statement
Dim strWhere As String ' Optional WHERE clause
Dim qryDef As DAO.QueryDef
Dim dbs As DAO.Database
strQryName = "qryAllOpenJobs"
strSql = "SELECT * FROM tblOpenJobs"
Set dbs = CurrentDb
' In case the query already exists we should deleted it
' so that we can rebuild it. The ObjectExists() function
' calls a public function in GlobalVariables module.
If ObjectExists("Query", strQryName) Then
DoCmd.DeleteObject acQuery, strQryName
End If
' Check to see if anything was selected from the Shift
' Drop down menu. If so, begin the where clause.
If Not IsNull(Me.cboShift.Value) Then
strWhere = "WHERE tblOpenJobs.[Shift] = '" & Me.cboShift.Value & "'"
End If
' Check to see if anything was selected from the Department
' drop down menu. If so, append or begin the where clause.
If Not IsNull(Me.cboDepartment.Value) Then
If IsNull(strWhere) Then
strWhere = strWhere & " AND tblOpenJobs.[Department] = '" & Me.cboDepartment.Value & "'"
Else
strWhere = "WHERE tblOpenJobs.[Department] = '" & Me.cboDepartment.Value & "'"
End If
End If
' Check to see if anything was selected from the Date
' field. If so, append or begin the Where clause.
If Not IsNull(Me.txtDate.Value) Then
If Not IsNull(strWhere) Then
strWhere = strWhere & " AND tblOpenJobs.[Date] = '" & Me.txtDate.Value & "'"
Else
strWhere = "WHERE tblOpenJobs.[Date] = '" & Me.txtDate.Value & "'"
End If
End If
' Concatenate the Select and the Where clause together
' unless all three parameters are null, in which case return
' just the plain select statement.
If IsNull(Me.cboShift.Value) And IsNull(Me.cboDepartment.Value) And IsNull(Me.txtDate.Value) Then
Set qryDef = dbs.CreateQueryDef(strQryName, strSql)
Else
strSql = strSql & " " & strWhere
Set qryDef = dbs.CreateQueryDef(strQryName, strSql)
End If
End Sub
2) Main Form where the user selects items from combo boxes.
picture of the main form and sub form
http://i48.tinypic.com/25pjw2a.png
3) Subform pointed at the query created in step 1.
Chain of events:
1) User selects item from drop down list on the main form.
2) Old query is deleted, new query is generated (same name).
3) Subform pointed at query does not update, but if you open the query by itself the correct results are displayed.
Name of the Query: qryAllOpenJobs
name of the subform: subQryAllOpenJobs
Also, the Row Source of subQryAllOpenJobs = qryAllOpenJobs
Name of the main form: frmManagement
I think you have your logic on the Department drop down check backwards.
You have it checking if strWhere is null, then if it is, you concatenate strWhere with the value of cboDepartment.
You should be doing what you are for Date.
' Check to see if anything was selected from the Department
' drop down menu. If so, append or begin the where clause.
If Not IsNull(Me.cboDepartment.Value) Then
If Not IsNull(strWhere) Then
strWhere = strWhere & " AND tblOpenJobs.[Department] = '" & Me.cboDepartment.Value & "'"
Else
strWhere = "WHERE tblOpenJobs.[Department] = '" & Me.cboDepartment.Value & "'"
End If
End If
You may also want to do:
If Nz(strWhere,"") = "" then
Instead of just doing IsNull so that you catch the zero-length string in addition to a null variable.
As for setting the recordsource, use something along the lines of
Me.sfrmJobs.Form.RecordSource = strSQL
where sfrmJobs is the name of your subform.
An empty string is not the same thing as Null. When you declare a String variable such as this:
Dim strWhere As String
strWhere is initialized as an empty string (or "zero length string"). That value is sometimes referred to as a null string, and there is even a VBA constant, vbNullString, which represents the empty string. However, regardless of which name you use, the empty string variable is not Null. Furthermore a VBA String variable can never be Null. For example, this code will cause error 94, 'Invalid use of Null':
Dim strWhere As String
strWhere = Null
The reason I am emphasizing this point is because your code tests whether strWhere is Null. That is a logic flaw because strWhere will never be Null. For example, I don't believe this condition can ever be True:
If IsNull(strWhere) Then
If you want a test to determine when strWhere has not had a value assigned to it (it's still an empty string), use the Len function:
If Len(strWhere) = 0 Then
Here is a different approach for BuildQuery. It assumes the data type for your [Date] field is String (as your original code suggests). If [Date] is actually Date/Time data type, this code will not work. Also, please note that Date is a reserved word (see Problem names and reserved words in Access). I enclosed the field name in square brackets to avoid ambiguity. If it were my own database, I would change the field name instead.
Private Sub BuildQuery()
'* Update subform RecordSource based on input from *'
'* the user on the Management tab. *'
Dim strSql As String ' Main SQL SELECT statement '
Dim strWhere As String ' Optional WHERE clause '
Dim i As Integer
Dim strControl As String
Dim strField As String
strSql = "SELECT * FROM tblOpenJobs AS oj"
strWhere = vbNullString
For i = 1 To 3
Select Case i
Case 1
strControl = "cboShift"
strField = "Shift"
Case 2
strControl = "cboDepartment"
strField = "Department"
Case 3
strControl = "txtDate"
strField = "[Date]"
End Select
If Not IsNull(Me.Controls(strControl).Value) Then
strWhere = strWhere & _
IIf(Len(strWhere) > 0, " AND ", "") & _
"oj." & strField & " = '" & _
Me.Controls(strControl).Value & "'"
End If
Next i
If Len(strWhere) > 0 Then
strSql = strSql & " WHERE " & strWhere
End If
'* use the name of the subform CONTROL for sfrmJobs *'
'* (may not be the name of the subform) *'
Me.sfrmJobs.Form.RecordSource = strSql
End Sub
My solution is below in three parts. (1) Build Query, (2) Main Form, (3) Subform.
`Private Sub OpenJobsQuery()
' This sub will construct the query on the front page for the user
' based on who they are and what they select from the combo boxes above
' the table for filtering by redefining the rowsource of the subform
' subQryOpenJobs
Dim strSql As String ' Main SQL SELECT statement
Dim strWhere As String ' Where clause containing user specified parameters.
strSql = "SELECT * FROM tblOpenJobs"
strWhere = ""
' Check to see if anything was selected from the Shift
' combo box. If so, begin the Where clause.
If Not IsNull(Me.cboOpenJobShift.Value) Then
strWhere = "WHERE tblOpenJobs.[Shift] = '" & Me.cboOpenJobShift.Value & "'"
End If
' Check to see if anything was selected from the Department
' combo box. If so, append or begin the where clause.
If Not IsNull(Me.cboOpenJobDepartment.Value) Then
If strWhere = "" Then
strWhere = "WHERE tblOpenJobs.[Department] = '" & Me.cboOpenJobDepartment.Value & "'"
Else
strWhere = strWhere & " AND tblOpenJobs.[Department] = '" & Me.cboOpenJobDepartment.Value & "'"
End If
End If
' Check to see if anything was selected from the Date
' field. If so, append or begin the Where clause.
If Not IsNull(Me.cboOpenJobDate.Value) Then
If strWhere = "" Then
strWhere = "WHERE tblOpenJobs.[JobDate] = #" & Me.cboOpenJobDate.Value & "#"
Else
strWhere = strWhere & " AND tblOpenJobs.[JobDate] = #" & Me.cboOpenJobDate.Value & "#"
End If
Else
' If nothing was entered in the date field, make sure the user
' only sees future jobs.
If strWhere = "" Then
strWhere = "WHERE tblOpenJobs.[JobDate] > #" & FormatDateTime(Date, vbShortDate) & "#"
Else
strWhere = strWhere & " AND tblOpenJobs.[JobDate] > #" & FormatDateTime(Date, vbShortDate) & "#"
End If
End If
' Always include as part of the where clause, a section that
' will narrow the results based on who the user is
If strWhere = "" Then
strWhere = "WHERE tblOpenJobs.[OpenJobID] Not In " & _
"(SELECT tblSignUps.[OpenJobID] FROM tblSignUps WHERE tblSignUps.[EUID] = '" & strEUID & "');"
Else
strWhere = strWhere & " AND tblOpenJobs.[OpenJobID] Not In " & _
"(SELECT tblSignUps.[OpenJobID] FROM tblSignUps WHERE tblSignUps.[EUID] = '" & strEUID & "');"
End If
' Concatenate the Select and the Where clause together
strSql = strSql & " " & strWhere
' Set the recordsource of the subform to the SQL query generated
' and refresh the form.
Me.subQryOpenJobs.Form.RecordSource = strSql
' In addition, synchronize the JobID's in the Edit Job box to match those
' filtered by this Build Query.
Me.cboSelectJOBID.RowSource = "SELECT tblOpenJobs.[OpenJobID] FROM tblOpenJobs" & " " & strWhere
Me.Refresh
End Sub`
(2) Main Form. http://j.imagehost.org/view/0385/Form. and (3) the subform is populated as shown in the BuildQuery() sub to construct the query based on what the user selects from the drop down filters and the input boxes on the right of the form. The data in the table itself is inaccessible to the user, this is just for them to reference.