I have a report that is opened and a filtered to a specific record by linking to piece of data. (Lot #)
Private Sub ServiceRequest_Click()
DoCmd.RunCommand acCmdSaveRecord
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "ServiceRequest"
stLinkCriteria = "[Lot_Number]=" & "'" & Me![Lot_Number] & "'"
DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
End Sub
What I need is an additional filter on the report to happen where [Trade] field is filtered to the selection on the originating form. [Trade] = "tradeselect.value"
I tried simply adding an additional stlinkCriteria, like this....
stLinkCriteria = "[Trade]=" & "'" & Me![TradeSelect] & "'"
but then nothing works. The form just opens on the first record, instead of being filtered to a particular one.
Any help greatly appreciated.
When combining two criteria, you need to do it just like an SQL statement (Field1 = "1" AND Field2 = "2")
For your case, you can just add the necessary text when adding the second criterium:
stLinkCriteria = stLinkCriteria & " AND [Trade]=" & "'" & Me![TradeSelect] & "'"
Related
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, "'", "''") & "'"
Sorry for any dumb questions, I am very green with Access and VBA.
I am creating a form for employees to input their hourly output. It goes to a table called tracking, where there are 4 fields; Shift, Operator, Date_Field, and Machine. These fields are a unique index, and I want it to work so that if the person filling out the form adds a date/shift/worker/machine combo that already exists it will just take them to that record.
This code runs On Change after each combobox is selected but it obviously has issues. Please let me know what I am doing wrong and how I could implement this.
Dim int_ID As Integer
If IsNull(DLookup("[ID]", "Tracking", "[Shift]='" & [Forms]![Tracking Form]![ShiftCbo] & "'" And "[Operator]='" & [Forms]![Tracking Form]![OpCbo] & "'" And "[Date_Field]='" & [Forms]![Tracking Form]![DateBox] & "'" And "[Machine]='" & [Forms]![Tracking Form]![MachineCbo] & "'")) = False Then
int_ID = DLookup("[ID]", "Tracking", "[Shift]='" & [Forms]![Tracking Form]![ShiftCbo] & "'" And "[Operator]='" & [Forms]![Tracking Form]![OpCbo] & "'" And "[Date_Field]='" & [Forms]![Tracking Form]![DateBox] & "'" And "[Machine]='" & [Forms]![Tracking Form]![MachineCbo] & "'")
DoCmd.GoToRecord acDataTable, "Tracking", acGoTo, int_ID
End If
End Sub
Use AfterUpdate instead of Change event.
First off, the AND operator needs to be within quote marks as it is literal text. If code is behind [Tracking Form], don't need full form reference. Code expects value of comboboxes to be text, not numbers, therefore fields must be text type. However, you have one field that appears to be date type and date parameters are defined with #.
Dim int_ID As Integer
With Me
int_ID = Nz(DLookup("ID", "Tracking", "Shift='" & .ShiftCbo & _
"' And Operator='" & .OpCbo & "' And Date_Field=#" & .DateBox & _
"# And Machine='" & .MachineCbo & "'"), 0)
End With
If int_ID <> 0 Then
DoCmd.GoToRecord acDataTable, "Tracking", acGoTo, int_ID
End If
A table has a column "Code" and trying to match from access form text box if the data in the textbox match with data in column "Code" it has to say yes data is exist.. request your kind assistance.i tried but seems to be wrong
ecode = Me.code.Text
Dim dupsql
dupsql = "SELECT Code FROM [BookingTable]WHERE Code ='" & ecode & "'"
'Debug.Print dupsql
If dupsql = ecode Then
MsgBox " The Entered Code is already in Use! ", vbInformation
end if
A SELECT SQL statement is used to open a recordset object. Your code does not open a recordset object. Just referencing variable that holds SQL string does nothing.
Don't need a recordset object. DLookup domain aggregate function can serve.
If Not IsNull(DLookup("Code", "BookingTable", "Code='" & Me.Code & "'") Then
MsgBox " The Entered Code is already in Use! ", vbInformation
End If
Or DCount.
If DCount("*", "BookingTable", "Code='" & Me.Code & "'") > 0 Then
I have used the following code and it works.
ecode = Me.code.Text
Dim datafind As String
datafind = Nz(DLookup("[Code]", "BookingTable", "Code = '" & ecode & "'"), 0)
Debug.Print datafind
If datafind = ecode Then
MsgBox " The Entered Code is already in Use! ", vbInformation
end if
A report is called from VBA to receive returned records from an Access pass-through query. After the DoCmd completes the report's parameters are set in the report's appropriate label containers setting their .Caption property as required. Access fails intermittently during this process which leads me to believe that the report is not truly open to receive the parameters. Here's the VBA sub:
Private Sub Report_Open(Cancel As Integer)
Dim strFromDate As String
Dim strToDate As String
Dim strWC As String
Dim intShift As Integer
Dim strSQL As String
strFromDate = InputBox("Enter From Date and Time: ")
strToDate = InputBox("Enter To Date and Time: ")
strWC = InputBox("Enter Work Center: ")
intShift = InputBox("Enter Shift: ")
strSQL = "exec dbo.uspWorkCentreReport_TEST " & "'" & strFromDate & "', " & "'" & strToDate & "', " & "'" & strWC & "', " & intShift & ";"
CurrentDb.QueryDefs("ptq_uspWorkCentreReport").SQL = strSQL
DoCmd.OpenReport "rpt_qry_ptq_uspWorkCentreReport", acViewReport
Me.lblFromDate.Caption = strFromDate
Me.lblToDate.Caption = strToDate
Me.lblWC.Caption = strWC
Me.lblShift.Caption = intShift
End Sub
When the failure occurrs VBA highlights the Me.lblFromDate.Caption = strFromDate. If I press Reset in VBA or End on the Run-time error '2467': dialog, Access abends without any other outward signs. Access then re-opens to save the copied *_Backupx.accdb and opens with a fresh copy of the .accdb. The error seems to be a standars MS error:
As I said the report is intermittent and when it fails VB always highlights the same line in code. How do I capture what is happening or can I make VB wait a half of full second before it tries to write the parameters?
As I remember, captions can not be modified, when report open. Only in design mode. So this is not correct, because you have already opened report
Me.lblFromDate.Caption = strFromDate
You should use text boxes instead of captions. Also you can clear the borders, fillings and so on, that text box will appear like a caption.
Finally the correct set of code was produced. The button click creates strOpenArgs and passes it with .OpenReport. The report opens and splits the OpenArgs and populates the appropriate labels with updated Captions. Text boxes would not work! Here's the button click event:
Private Sub btnPreviewP1_Click()
If (Me.txtToDateP1 < Me.txtFromDateP1) Then
MsgBox ("The From Date must occurr before the To Date!")
End If
Dim strFromDateHMS As String
Dim strToDateHMS As String
Dim strSQLP1 As String
Dim strOpenArgs As String
strFromDateHMS = Format(Me.txtFromDateP1, "yyyy-mm-dd") & " " & Me.cboFromHourP1 & ":" & Me.cboFromMinuteP1 & ":" & Me.cboFromSecondP1
strToDateHMS = Format(Me.txtToDateP1, "yyyy-mm-dd") & " " & Me.cboToHourP1 & ":" & Me.cboToMinuteP1 & ":" & Me.cboToSecondP1
strSQLP1 = "exec dbo.uspWorkCentreReport '" & strFromDateHMS & "','" & strToDateHMS & "','" & strWCP1 & "'," & strShiftP1
strOpenArgs = Me.RecordSource & "|" & strFromDateHMS & "|" & strToDateHMS & "|" & strWCP1 & "|" & strShiftP1
' This line is all that's needed to modify the PT query
CurrentDb.QueryDefs("ptq_uspWorkCentreReport").SQL = strSQLP1
DoCmd.OpenReport "rpt_ptq_uspWorkCentreReport", acViewReport, , , , strOpenArgs
End Sub
And here's the reports _Open:
Private Sub Report_Open(Cancel As Integer)
Dim SplitOpenArgs() As String
SplitOpenArgs = Split(Me.OpenArgs, "|")
Me.lblFromDate.Caption = SplitOpenArgs(1)
Me.lblToDate.Caption = SplitOpenArgs(2)
Me.lblWC.Caption = SplitOpenArgs(3)
Me.lblShift.Caption = SplitOpenArgs(4)
End Sub
This opens the report every time with new appropriate data, so long as the report is closed before the form's button is pressed again for another refresh of the report. If the report is not closed the report stays up with the original data and does not refresh with new data... But that is another question I am about to ask. Thanks All.
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