How to pass a combobox as a parameter in Access? - ms-access

I have the following example code on a few events for a few comboboxes:
Private Sub cbo_oc_tours_all_Enter()
' Find the record that matches the control.
Dim rs As Object
Set rs = Me.Recordset.Clone
rs.FindFirst "[ID_T_OC] = " & Str(Nz(Me![cbo_oc_tours_all], Null))
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
Me.cbo_oc_tours_today.Value = Str(Nz(Me![cbo_oc_tours_all], Null))
End Sub
I want to put this into a single function and call it from each event procedure(?). I've used the following code in the function:
Sub goto_record(goto_rec As ComboBox, update_rec As ComboBox)
Dim rs As Object
Set rs = Me.Recordset.Clone
rs.FindFirst "[ID_T_OC] = " & Str(Nz(Me![goto_rec], Null))
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
update_rec.Value = Str(Nz(Me![goto_rec], ""))
End Sub
And I'm trying to call it using:
Sub cbo_oc_tours_all_Enter()
Call goto_record(cbo_oc_tours_all, cbo_oc_tours_today)
End Sub
However, I'm getting an error saying Microsoft Access can't find the field 'goto_rec' referred to in your expression. I'm clearly not passing the combo box in the right way but it's been a while since I've used VBA. What's my issue?

Do not pass the combo box as an object, just pass the value in the combo box. Here is an example:
Sub cbo_oc_tours_all_Enter()
Call goto_record(me.cbo_oc_tours_all, me.cbo_oc_tours_today)
End Sub
Assuming those are the names of the combo boxes themselves. Then you other function would be
'Using a data type of variant for example. Replace with the datatype you are using
Sub goto_record(goto_rec As Variant, update_rec As Variant)
Dim rs As Recordset
Set rs = Me.Recordset.Clone
rs.FindFirst "[ID_T_OC] = " & Str(Nz(goto_rec,0))
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
'You can change this line to this if the function is in the same module as the form with the combobox
me.cbo_oc_tours_today = goto_rec
End Sub

Related

FormOpen Event procedure not triggered with command button, but trigged when switching from design to form view in MS Access

I have an MS Access database with a set of forms to enter vegetation data for a large monitoring project. I have one form called frmTransect with a button that opens a second form called frmLPI which is set up as an unbound main form with a subform called frmLPIDetail bound to a sql server database table. The main form has just two unbound fields, DataObs and DataRec, both of which are comboboxes. These two field are set up with an AfterUpdate event procedure to populate their corresponding fields in the subform, Data_observer and Data_recorder. This works perfectly. I wanted to have the unbound fields autopopulate with the last value in the subform of Data_observer and Data_recorder when the form is lauched again. To do this I used a FormOpen event procedure. Below is the code:
Private Sub Form_Open(Cancel As Integer)
Me.TransectOID = Me.OpenArgs
Dim rs As DAO.Recordset
Set rs = Me!frmLPIDetail.Form.RecordsetClone
If rs.RecordCount > 0 Then
If Not rs.BOF Then
rs.MoveLast
rs.MovePrevious
End If
If Not IsNull(rs!Data_recorder.Value) Then
Me.DataRec.Value = rs!Data_recorder.Value
Me.frmLPIDetail.Form.Data_recorder.DefaultValue = """" & Me.DataRec.Value & """"
End If
If Not IsNull(rs!Data_observer.Value) Then
Me.DataObs.Value = rs!Data_observer.Value
Me.frmLPIDetail.Form.Data_observer.DefaultValue = """" & Me.DataObs.Value & """"
End If
rs.MoveLast
rs.MoveFirst
While Not rs.EOF
rs.Edit
rs!Data_recorder.Value = Me.DataRec.Value
rs!Data_observer.Value = Me.DataObs.Value
rs.Update
rs.MoveNext
Wend
End If
rs.Close
Set rs = Nothing
Me.Dirty = False
End Sub
Here is where things get weird. When I click the command button on frmTransect, frmLPI opens, but the FormOpen event procedure above doesn't get launched. However, if I switch into design view, and then back into Form View, it does trigger, and works as intended! How can I get this event procedure to launch when I open the frmLPI using the command button in frmTransect? Any help would be much appreciated.
And it turned out it was as simple as adding a Me.Refresh and Me.Requery to my code block:
Private Sub Form_Open(Cancel As Integer)
Me.TransectOID = Me.OpenArgs
Me.Refresh
Me.Requery
Dim rs As DAO.Recordset
Set rs = Me!frmLPIDetail.Form.RecordsetClone
If rs.RecordCount > 0 Then
rs.MoveLast
rs.MovePrevious
If Not IsNull(rs!Data_recorder.Value) Then
Me.DataRec.Value = rs!Data_recorder.Value
Me.frmLPIDetail.Form.Data_recorder.DefaultValue = """" & Me.DataRec.Value & """"
End If
If Not IsNull(rs!Data_observer.Value) Then
Me.DataObs.Value = rs!Data_observer.Value
Me.frmLPIDetail.Form.Data_observer.DefaultValue = """" & Me.DataObs.Value & """"
End If
rs.MoveLast
rs.MoveFirst
End If
rs.Close
Set rs = Nothing
Me.Dirty = False
End Sub

How can I use a Listbox Control to Correctly Handle Entry of a New Record?

I have a listbox on my form which gives me an error.
The ProductNo field is the primary key.
This error happens when I partially enter a new record and decide to navigate away from the ProductNo control to the listbox item.
Bellow is my current nightmare:
Private Sub InventoryListBox_AfterUpdate()
' Find Record that matches the control.
Dim rst As Object
Set rst = Me.Recordset.Clone
rst.FindFirst "[ProductNo] = '" & Me![ListBox] & "'"
If Not rst.EOF Then
Me.Bookmark = rst.Bookmark 'Error here!
End If
End Sub
Test the NoMatch property instead of the EOF property, e.g.:
Private Sub InventoryListBox_AfterUpdate()
Dim rst As Recordset
Set rst = Me.RecordsetClone
rst.FindFirst "[ProductNo] = '" & Me![ListBox] & "'"
If Not rst.NoMatch Then
Me.Bookmark = rst.Bookmark
End If
End Sub
The above assumes that ProductNo is a string-valued field.

Access - VBA - Get row number by criteria

Looking at the following code, how can I receive the row number for a user using the variable usern?
I can check whether a user exists using the DCount function as can be seen. Once received the row number, I would like to use it to navigate to that entry using DoCmd.GoToRecord. GoToRecord itself works already. I just cannot find a way to receive the row number...
Private Sub Form_Current()
Dim usern As String
Dim count As Integer
usern = Environ("Username")
count = DCount("name_", "Fragebogen", "name_='" & usern & "'")
DoCmd.GoToRecord acDataForm, "Fragebogen", acGoTo, 3
End Sub
Have you tried the FindFirst method?
Locates the first record in a dynaset - or - snapshot-type Recordset
object that satisfies the specified criteria and makes that record the
current record.
Dim rs As Recordset
Set rs = Me.RecordsetClone
rs.FindFirst "name_ = '" & Environ("Username") & "'"
If Not rs.NoMatch Then
Me.Bookmark = rs.Bookmark
Else
MsgBox "No match was found.", vbExclamation
Emd If

search sub form from text box in form

I have a form (frm_main)
and i have a subform on this form (frm_weblogs_subform)
I am trying to search the subform using a text box.
the code i've found and have been playing with to no avail is:
Private Sub find_weblog_button_Click()
Dim D As Database
Dim wlog As DAO.Recordset
Dim Criteria As String
Set D = CurrentDb
Set wlog = D.OpenRecordset("form_frm_weblogs_subform", dbOpenDynaset)
Criteria = "[weblog_number]='" & [weblogSearch] & "'"
wlog.FindFirst Criteria
wlog.Close
End Sub
It doesn't seem to register the form at all it keeps saying it can't be found.
can anyone help point me in the right direction?
It should read:
Dim wlog As DAO.Recordset
Dim Criteria As String
Set wlog = Me!<NameOfTheSubformCONTROL>.RecordsetClone
If wlog.RecordCount > 0 Then
Criteria = "[weblog_number]='" & [weblogSearch] & "'"
wlog.FindFirst Criteria
If wlog.NoMatch = False
' Found.
Else
' Not found.
End If
End If
wlog.Close
Replace <NameOfTheSubformCONTROL> with that, not the name of the subform.

List Box items not highlighted when clicking on them

I have a list box in a form. Clicking on it causes the form to jump to another record. It supposed to highlight an item and jump to the correct record. Instead, it highlights, and then instantly clears the selection, although it still jumps to the record. When I use standard record selection buttons, items are correctly highlighted.
I read the index of selected item from .ListIndex property because Selected() does not work in a Single Selection mode when I test which item is selected. However, .ListIndex is read-only property and I use .Selected() to highlight the item.
Option Compare Database
Option Explicit
Private Sub Form_Current()
Call highlightListBox
End Sub
Private Sub lbListBox_Click()
Dim rs As DAO.Recordset
Dim indx As Long
Set rs = Me.RecordsetClone
If Not rs.BOF And Not rs.EOF Then
rs.MoveFirst
rs.FindFirst "[ID]=" & CStr(Me.lbListBox.ItemData(Me.lbListBox.ListIndex))
If Not rs.NoMatch Then
Me.Bookmark = rs.Bookmark
End If
End If
rs.Close
Set rs = Nothing
End Sub
Private Sub highlightListBox()
Dim lngIndx As Long
Dim lngI As Long
Dim bNoMatch As Boolean
lngIndx = 0
bNoMatch = True
If Me.NewRecord <> 0 Or IsNull(Me!ID) Then
For lngI = 0 To Me.lbListBox.ListCount - 1
Me.lbListBox.Selected(lngI) = False
Next lngI
Else
Do
lngIndx = lngIndx + 1
If CLng(Me.lbListBox.ItemData(lngIndx - 1)) = Me!ID Then
bNoMatch = False
End If
Loop Until CLng(Me.lbListBox.ItemData(lngIndx - 1)) = Me!ID Or lngIndx = Me.lbListBox.ListCount
End If
If Not bNoMatch Then
Me.lbListBox.Selected(lngIndx - 1) = True
End If
End Sub
I have been given a suggested about slightly different problem here but thanks to Remou I sorted this out.
The new code is following:
Option Compare Database
Option Explicit
Private Sub Form_Current()
Me.lbListBox = Me!ID
End Sub
Private Sub lbListBox_Click()
Dim rs As DAO.Recordset
Dim indx As Long
Set rs = Me.RecordsetClone
If Not rs.BOF And Not rs.EOF Then
rs.MoveFirst
rs.FindFirst "[ID]=" & CStr(Me.lbListBox.ItemData(Me.lbListBox.ListIndex))
If Not rs.NoMatch Then
Me.Bookmark = rs.Bookmark
End If
End If
Me.lbListBox = Me!ID
rs.Close
Set rs = Nothing
End Sub
I did not realise I could actually set a value to a list box using BoundColumn. By doing so, both highlighting and focusing is set. I am not sure but I think that MultiSelection has to be set to 0. In my case, the line
Me.lbListBox = Me!ID
does the job :)
I hope this answer can help someone else :)