Okay I am working with Access 2021, VBA programming ist more than a decade ago so I need some basic help.
I'm having
a table tblMyData with the fields ID, Name, givenName
a form frmMyForm with those fields
a pop-up search box which gives the search results in a list box 'lstItems'
if I double-click on a list item, Me.lstItems.Value gives me the actual ID of the record, not the record number.
Now I want the underlying form (frmMyForm) to go to the recordset with this ID.
frmMyForm.FindFirst "[ID] LIKE 'Me.lstItems.Value'"
(which throws me an error, [variable not defined])
or something like
DoCmd.GoToRecord acDataForm, "frmMyForm", acGoTo, Me.lstItems.ID
(which throws me an error, [method or variable not defined])
I tried any combination of the above objects, but cannot reside.
If your ID is numeric:
Me.Recordset.FindFirst "[ID] = " & Me.lstItems.Value ' E.g. "[ID] = 7"
If it is a text:
Me.Recordset.FindFirst "[ID] = '" & Me.lstItems.Value & "'" ' E.g. "[ID] = 'abc'"
Related
I have a form control (address) that uses Dlookup to call info from a table "database" but the form is bound to table "Tracker". The dlookup is based on another control on the same form - "Name" I need the form to record this dlookup control, along with other controls that are bound to "tracker" as new recordto the table "tracker."
My failed attempts:
Using the default value property to assign the recalled data from the dlookup to another text box which would be bound to "tracker" This simply does not work for some reason. Perhaps I am missing something that tells this control "Address" to update upon selecting the correct "name?"
Code:
Private Sub SubmitReferral_Click()
On Error GoTo Err_SubmitReferral_Click
DoCmd.GoToRecord , , acNewRec
Exit_SubmitReferral_Click:
Exit Sub
Err_SubmitReferral_Click:
MsgBox Err.Description
Resume Exit_SubmitReferral_Click
End Sub
I also tried this - to assign the data - but the data from the dlookup in control "Address1" is not transferring/copying to control "Address2"
Private Sub Combo276_OnUpdate()
OnUpdate ([Address2].Value = [Address1].Value)
End Sub
Help or suggestions?
PS - I have tried to Edit per request to be as specific as possible, and to follow proper board etiquette.
Still unsure of your field names, etc., but the following is an example you can modify. Change 'tblEmployee' to 'database'.
I must state that if you are just starting out with developing in Access (or VBA) that you should never use names that are reserved words, or that can be misleading. Your table named 'database' is ok if named 'tblDatabase'.
Option Compare Database
option Explicit
Private Sub cmdInsert_Click()
Dim strSQL As String
Dim i As Integer
Debug.Print "cmdInsert; "
i = MsgBox("Do you want to add 1 row for Employee ID: " & Me.EmpID & " to table 'tracker'?", vbYesNo, "Confirm Add")
If i = vbNo Then
Exit Sub
End If
DoCmd.SetWarnings True
strSQL = "INSERT INTO tracker ( FirstName, LastName, Add1, City, St, Zip ) " & _
"SELECT tblEmployee.FirstName, tblEmployee.LastName, tblEmployee.Add1, tblEmployee.City, tblEmployee.St, tblEmployee.Zip " & _
"FROM tblEmployee " & _
"WHERE (((tblEmployee.EmpID)=" & Me.EmpID & "));"
DoCmd.RunSQL strSQL
End Sub
Thanks for the help - I solved my concern by hiding the fields that contain the dlookup, and putting code behind a button that copies the information to fields that are bound and therefore will record to the table "tracker"
Filters in Access seem to be 'sticky' - when you set one with VBA you can remove it but you can't set a different one.
I have a Access database for tracking student scores. It has tables subjects, teachers, students, tests and test_results. Each results record refers to a student and a test.
I have a form displaying tests with a subform displaying results. I want to search for tests using various criteria so I added some unbound fields to the (outer) form header and labelled them 'name', 'subject', 'start date', 'end date' and 'teacher'. I added a 'filter' button and a 'reset' button. Each search field is optional so any combination can be used: any left blank will be ignored.
This is the code for the filter button:
Me.Filter =
"([Forms]![testWithResults]![Text102] IS NULL OR test_name Like '*' & [Forms]![testWithResults]![Text102] & '*')
AND ([Forms]![testWithResults]![Combo89] IS NULL OR teacher = [Forms]![testWithResults]![Combo89])
AND ([Forms]![testWithResults]![Combo52] IS NULL OR subject = [Forms]![testWithResults]![Combo52])
AND ([Forms]![testWithResults]![Text83] IS NULL OR [Forms]![testWithResults]![Text85] IS NULL OR test_date BETWEEN [Forms]![testWithResults]![Text83] AND [Forms]![testWithResults]![Text85])"
Me.FilterOn = True
This is the code for the reset button:
Me.FilterOn = False
Me.Combo89 = Me.Combo89.DefaultValue
Me.Combo52 = Me.Combo52.DefaultValue
Me.Text83 = Me.Text83.DefaultValue
Me.Text85 = Me.Text85.DefaultValue
Me.Text102 = Me.Text102.DefaultValue
When I first load the form, the first time I search it all works perfectly. The filter button works just as expected and the reset button empties all fields and displays all records. But when I try to search again with new criteria I just get my old results again. To make it work I have to close and reopen the form.
When I replaced Me.Filter with DoCmd.ApplyFilter it still worked perfectly the first time but the second time I would get an error 'the expression is too complex to be evaluated'.
Since Access complains the Filter string is too complex, simplify it.
You want to base a Filter condition on a text box. At the time you create the Filter string, your code can check whether that text box is Null. If it is not Null, add a condition based on the text box's value. If it is Null, the Filter can simply ignore that text box.
Dim strFilter As String
With [Forms]![testWithResults]
If Not IsNull(![Text102]) Then
strFilter = strFilter & " AND test_name Like '*" & ![Text102] & "*'"
End If
If Not IsNull(![Combo89]) Then
strFilter = strFilter & " AND teacher = " & ![Combo89]
End If
If Not IsNull(![Combo52]) Then
strFilter = strFilter & " AND subject = " & ![Combo52]
End If
If Not (IsNull(![Text83]) Or IsNull(![Text85])) Then
strFilter = strFilter & " AND test_date BETWEEN " & Format(![Text83], "\#yyyy-m-d\#") _
& " AND " & Format(![Text85], "\#yyyy-m-d\#")
End If
End With
If Len(strFilter) > 0 Then
' use Mid() to discard leading " AND "
Debug.Print Mid(strFilter, 6) '<- view this in Immediate window; Ctrl+g will take you there
Me.Filter = Mid(strFilter, 6)
Me.FilterOn = True
Else
MsgBox "no conditions for Filter"
End If
I am creating a user interface based off of one internal and two linked (external) Access datasheets in Access 2013.
Two of the fields on my UI are combo boxes that read from the linked datasheets and display the options. This is so that the entries for suppliers and material types are called-out consistently and typos are avoided. However, I would like to add the following functionality:
-If a new value is entered into the combo box the user will be prompted to fill out the necessary information on the new value. This information will subsequently be saved to the appropriate linked datasheet.
How would I go about setting up the prompt from the combo boxes themselves? It would require Access to open a form or sub-form that will, in turn, save to the linked datasheet.
I'd prefer it to be automatic, instead of end-user prompted so that it isn't skipped. It's been years since I played around with VB, so I would like to avoid that if possible and use Access' built-in functions (even if it requires a little more time). Thank you in advance!
Alright, so I was able to do it after researching the "OnNotInList" function and a little VB code.
In the OnNotInList section of the 'Event' properties sheet, I chose 'Code Builder' and entered the following:
Private Sub Supplier_NotInList(NewData As String, Response As Integer)
Dim ctl As Control
Dim dbsCustomerDatabase As Database
On Error GoTo Supplier_NotInList_Err
Dim intAnswer As Integer
Dim strSQL As String
intAnswer = MsgBox("The supplier " & Chr(34) & NewData & _
Chr(34) & " is not currently listed." & vbCrLf & _
"Would you like to add it to the list now?" _
, vbQuestion + vbYesNo, "Spire Manufacturing Solutions")
' Adding the new entry to the list:
If intAnswer = vbYes Then
strSQL = "INSERT INTO CustomerList([CustomerName]) " & _
"VALUES ('" & NewData & "');"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
MsgBox "The new supplier has been added to the list." _
, vbInformation, "Spire Manufacturing Solutions"
Response = acDataErrAdded
' Opening the Supplier datasheet to add details
' to the new entry:
MsgBox "Opening Supplier database for new entry..."
DoCmd.OpenTable "CustomerList", acViewNormal, acEdit
End If
Supplier_NotInList_Exit:
Exit Sub
Supplier_NotInList_Err:
MsgBox Err.Description, vbCritical, "Error"
Resume Supplier_NotInList_Exit
End Sub
This allowed me to automatically prompt the user to add the details for a new supplier if they enter a new supplier name. Or, cancel the entry if they simply misspelled it. I'd quite forgotten how versatile VB was. Thank you all for your assistance in getting me headed in the right direction!
I need to let users filter a continuous form using values the user enters into a textbox. And the continuous form is also nested within a couple levels of navigation subforms. This sounds easy enough, but all the examples I find on the web use macros instead of vba.
I set up the structure and wrote an AfterUpdate procedure for a textbox txtFilter as follows:
Private Sub txtFilter_AfterUpdate()
Dim filterval As String
filterval = txtFilter.Value
With Forms!Main!NavigationSubform.Form!NavigationSubform.Form
.Filter = "LastName Like " & filterval
.FilterOn = True
End With
End Sub
I have played with different syntax, but none of it seems to work properly. Here is a link to download the relevant parts of the database from a file sharing site: http://jmp.sh/v/HGctZ4Ru74vDAjzN43Wq
Can anyone show me how to alter this so that users can use the textbox to filter the continuous form?
I got it to work using this: .Filter = "LastName Like """ & filterval & """"
Need those annoying String Identifiers even for strings sometimes.
Okay, To get the form to open with no records and then pull up just the records you (or the user) specifies is easiest with a bit of re-work.
(I'd recommend you working with a copy and not your original)
1:On your Continuous Form, remove the Recordsource; we're going to use Late Binding (Kinda)
2:Then delete the code under the txtFilter box, then delete the box itself.
3:Add a comboBox with something like this as the recordsource:
SELECT DISTINCT myTable.LastName FROM myTable ORDER BY myTable.LastName; (This will get you a unique list of last names so knowing how to spell the name will not be necessary, plus it assures at least one match)
4:In the After Update event of that combobox, add code like this:
Dim strSource As String
strSource = "SELECT mt.IntakeNumber, mt.ClientNumber, " & _
"mt.LastName, mt.FirstName, mt.ConsultationDate " & _
" FROM myTable mt " & _
"WHERE (mt.LastName)= '" & Me.cboFilter.Value & "'"
Me.RecordSource = strSource
Me.Requery
Obviously you'll need to change the table and field names as necessary, but hopefully you get the idea.
Option Compare Database
Option Explicit '<- always include this!!!!!
Private Sub txtFilter_AfterUpdate()
Dim strFilter As String
' only set Filter when text box contains something
' to search for ==> don't filter Null, empty string,
' or spaces
If Len(Trim(Me.txtFilter.Value & vbNullString)) > 0 Then
strFilter = "LastName Like '*" & _
Replace(Me.txtFilter.Value, "'", "''") & _
"*'"
' that Replace() prevents the procedure from breaking
' when the user enters a name with an apostrophe
' into the text box (O'Malley)
Debug.Print strFilter ' see what we built, Ctrl+g
Me.Filter = strFilter
Me.FilterOn = True
Else
' what should happen here?
' maybe just switch off the filter ...
Me.FilterOn = False
End If
End Sub
I have a list box that displays my entire Access databases records with a brief descriptions in additional columns. The first column (0) has the record identifier (PART NUMBER) which is the primary key in the table. I am trying to get the double click event to open the "SETUP SHEET DATA ENTRY" Form to the specific row in the list box. The part number is classified as text and not numerical. I don't know what the problem is and any advice would help.
List box name = Listallpart
Primary key in table name = PART NUMBER
Table name = Setup Sheet History
Form name = Setup Sheet Data Entry
Private Sub Listallpart_DblClick(Cancel As Integer)
DoCmd.OpenForm "SETUP SHEET DATA ENTRY", , , "[PART NUMBER] = " & Me.Listallpart.Column(0).Value
End Sub
I have another question very similar....
Private Sub Listallpart_DblClick(Cancel As Integer)
Dim strpn As String
Dim strco As String
strpn = Me.Listallpart.Column(0)
strco = Me.Listallpart.Column(1)
DoCmd.OpenForm "SETUP SHEET DATA ENTRY", , , ("[PART NUMBER] = '" & Me.Listallpart.Column(0) & "'" And "[CURRENTOPERATION] ='" & Me.Listallpart.Column(1) & "'")
End Sub
The code above is for another mod to the code. Every record has these 2 required unique fields. I am getting a "run time error "13" mismatch type". I added the two variables in the beginning to make sure the values are correct. I inserted break points and confirmed the values. The data is numerical.
Since [PART NUMBER] is a text field, add quotes around the list box value in your OpenForm statement.
DoCmd.OpenForm "SETUP SHEET DATA ENTRY", , , "[PART NUMBER] = '" & Me.Listallpart.Column(0).Value & "'"
Your second question is the inverse of your first question; seeing as [CURRENTOPERATION] is numeric you don't need to add the quotes to this in your OpenForm
DoCmd.OpenForm "SETUP SHEET DATA ENTRY", , , ("[PART NUMBER] = '" & Me.Listallpart.Column(0) & "'" And "[CURRENTOPERATION] =" & Me.Listallpart.Column(1))