Move to another record via combobox doesn't work in some circumstances - ms-access

I have two forms: ‘frmClient’, (which has a subform that lists applicants), and ‘frmDisclosure’, which shows details of applicants. On frmClient there is a command button that opens a specified record in frmDisclosure. The procedure is Private Sub Command10_Click() - see below. This works.
The problem is that once in frmDisclosure via frmClient, it is not possible to move to another record. The procedure for opening another record in frmDiscloure is in a combobox control: Private Sub ComboFind_AfterUpdate().
This normally works, but it never works if frmDiscloure has been opened via frmClient. I have tried ‘requery’ and ‘refresh’ in various situations, and have tried closing frmClient once frmDisclosure is open. None of this works. If I want to get to a different record, the only solution I have at present is to close frmDisclosure and reopen it.
\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Private Sub Command10_Click()
If NumForms > 0 Then
DoCmd.OpenForm "frmDisclosure"
Forms!frmDisclosure.FilterOn = False
DoCmd.OpenForm "frmDisclosure", acNormal, "", "[DiscPK]=" & Me.DiscPK, , acNormal
Else
DisplayMessage ("No form ref for this application.")
Exit Sub
End If
End Sub
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Private Sub ComboFind_AfterUpdate()
Dim rs As Object
Set rs = Me.RecordsetClone
rs.FindFirst "[DiscPK] = " & Str(Nz(Me![ComboFind], 0))
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
End Sub
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

frmDisclosure is opened to a single record, there are no other records to navigate. The RecordsetClone has only one record, so of course code won't find any others. Turn off the filter first:
Private Sub ComboFind_AfterUpdate()
Me.FilterOn = False
With Me.RecordsetClone
.FindFirst "[DiscPK] = " & Nz(Me.ComboFind, 0)
If Not .NoMatch Then Me.Bookmark = .Bookmark
End With
End Sub
As you can see, declaring and setting a recordset object variable is not required. .EOF would probably work just as well, I have just always used NoMatch. This will set focus to record, not filter the form.
If you prefer to display single record, then set the Filter property.
Private Sub ComboFind_AfterUpdate()
Me.Filter = "DiscPK=" & Nz(Me.ComboFind, 0)
End Sub

Related

MS Access Recordset Moving on Search even if no match found?

This is driving me nuts. I have an access VBA form where I want to allow users to type a document number into a box in order to jump directly to that record.
The underlying table as a field "DocNum", and the number is always sequential. I want the searchbox to display the current Doc Number the user is on, if they type in a different number and hit enter, it should either jump to that record if it exists, or if it doesn't exist, stay on the current record.
I'm trying to accomplish thisby having a hidden textbox bound to "DocNum", and an unbound visible box on top of it. On Form_Current() the unbound box is made to match the underlying field. After, update I run the following code to perform the search.
Private Sub txt_DocNumSearch_AfterUpdate()
Dim rs As Object
Set rs = Me.RecordsetClone
With rs
rs.FindFirst "[DocNum] = " & Str(Me![txt_DocNumSearch])
If rs.NoMatch Then
MsgBox "Record not found."
GoTo Cleanup
Else
Me.Bookmark = rs.Bookmark
Exit Sub
End If
End With
Cleanup:
rs.Close
Set rs = Nothing
Set dbs = Nothing
End Sub
What's driving me insane is that even when it doesn't find a match.... it skips to the next record anyway. No matter what I do... move last, trying to bookmark before I even search, etc... I can't get it to not jump forward. What am I missing?
Try simplifying it:
Private Sub txt_DocNumSearch_AfterUpdate()
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
With rs
.FindFirst "[DocNum] = " & Str(Me![txt_DocNumSearch])
If .NoMatch Then
MsgBox "Record not found."
Else
Me.Bookmark = .Bookmark
End If
.Close
End With
End Sub

How to notify when a checkbox is checked

Change on checkbox is not recognized when the check is performed by custom function.
We have form where checkboxes can be clicked. In this case the record will be added to a list, which will be updated with a save function. And there is mentioned function to check all check boxes, but this does not behave like when i click an individual checkbox.
code "Select all"
Private Sub Befehl83_Click()
With Me.RecordsetClone
.MoveFirst
Do Until .EOF
.Edit
!visited = True
.update
.MoveNext
Loop
End With
End Sub
clicked on checkbox
Private Sub chkVisited_Click()
If Not visitedList.Contains(Me.Form.Recordset.Fields("trainingMeasureID").Value) Then
visitedList.Add Me.Form.Recordset.Fields("trainingMeasureID").Value
Else
visitedList.Remove Me.Form.Recordset.Fields("trainingMeasureID").Value
End If
End Sub
Currently the state of checkboxes is not saved when used the select-all function. Only when a checkbox is selected individually.
It should save correctly in both cases.
Here's a not so ideal solution:
Private Sub Befehl83_Click()
With Me.Recordset
.MoveFirst
Do Until .EOF
.Edit
.Fields("visited") = True
.Update
Call chkVisited_Click()
.MoveNext
Loop
End With
End Sub
This will actively update the record set, you'll actually see the active record moving in the form.
Another solution would be to rebuild your code and do your updates directly to the underlying data source based on your form's recordset. Then update your list accordingly.
According to the docs, RecordsetClone is a read-only copy of the form's recordset. https://learn.microsoft.com/en-us/office/vba/api/access.form.recordsetclone
Use the AfterUpdate event:
Private Sub Befehl83_Click()
With Me.RecordsetClone
.MoveFirst
Do Until .EOF
If !visited.Value = False Then
.Edit
!visited.Value = True
.update
End If
.MoveNext
Loop
End With
End Sub

Access report - Recordsetclone as. Recordsource

I have a form, which has button for report. I want to set Report .Recordsource to whatever on screen is, so basically I need .RecordsetClone of form send to Report. Here is what I tried, but It doesn't work:
Me.Recordsource= Forms!Myform.RecordsetClone
I get an invalid argument on that. Any ideas how to solve this ?
EDIT:
I tried this too - this button is placed on form which has records and opens Report :
Private Sub cmdOpenReport_Click()
DoCmd.OpenReport "MyReport", acViewReport
Reports![MyReport].RecordSource = Me.RecordSource
Reports![MyReport].Filter = Me.Filter
Reports![MyReport].FilterOn = True
End Sub
You can't do that, but you may get away with:
Me.RecordSource = Forms!Myform.RecordSource
though that will not include a filter applied to the form. However, the Filter can be copied the same way, and then:
Me.Filter = Forms!Myform.Filter
Me.FilterOn = True
while sorting must be specified in the report the usual way.
Proof of concept
Private Sub Report_Open(Cancel As Integer)
If MsgBox("Mod 2?", vbQuestion + vbYesNo, "Select") = vbYes Then
Me.RecordSource = "Select * From TestTable Where Id Mod 2 = 0"
End If
End Sub
Gustav, this is correct answer. I have opened another thread for that, but I wasn't aware of what is wrong and where. Sorry for crossposting. here is link to my thread:
Access Report - show current recordsource of another form
Dim strWhere As String
Me.Dirty = False
With Me.Recordset.Clone
Do Until .EOF
strWhere = strWhere & "," & !ID
.MoveNext
Loop
End With
strWhere = Mid(strWhere, 2)
DoCmd.OpenReport "MyReport", acViewReport, WhereCondition:="ID In (" & strWhere & ")
Moderators can delete one of the threads, I can't do that.

Docmd.Close not closing the access form

I am experiencing a problem with vb in access. There is main form (say it parentForm), which has two buttons that trigger two different forms (say it childForm1 and childForm2). Both child forms perform checks on their Load events. If a condition fails, the childForm has to close. The problem is that in childForm1, the code works properly, in the childForm2 something goes completely wrong.
It seems that the close event is totally ignored. After the onLoad event, the process is carried out to the onCurrent event, which shouldn't happen! Below is the code of the onLoad event of childForm2.
Private Sub Form_Load()
On Error Resume Next
Dim db As Database
Dim rst As Recordset
Dim stDocName As String
stDocName = "childForm2"
closeEvent = False
Set db = CurrentDb
If a<> 0 And b<> 0 Then
Set rst = db.OpenRecordset("SELECT * FROM tbl1 WHERE Cust Like '*" & a & "*' AND Cust2 Like '*" & b & "*';")
If (rst.EOF Or rst.BOF) And IsLoaded(stDocName) Then
MsgBox ("No record found!")
rst.Close
SetWarnings = True
closeEvent = True
Me.SetFocus
DoCmd.Close acForm, stDocName, acSaveNo
End If
ElseIf a = 0 And b <> 0 Then
Set rst = db.OpenRecordset("SELECT * FROM tbl1 WHERE Cust2 Like '*" & b & "*';")
If (rst.EOF Or rst.BOF) Then
MsgBox ("No record found!")
rst.Close
DoCmd.Close acForm, stDocName
End If
End If
db.Close
End Sub
Also, I tried to use a global boolean variable (closeEvent in code), which is initialized to False and gets True, when the form must close. This variable is checked in the onCurrent event in order to close the form. However, when i debugged the code, the variable seems to lose its (True) value when passing from onLoad to OnCurrent event!
Any suggestion is more than appreciated.
Thanks in advance,
Maria
Use the Form_Open event instead of the Form_Loadevent.
Then, instead of closing the form (=Docmd.Close) use the built in Cancel argument to cancel the form's opening.
Private Sub Form_Open(Cancel As Integer)
If **condition not met** then
Cancel = True 'this stops the form from opening
End If
End Sub

Auto Populate Access Form using simple VBA code by setting a variable

I was recently given the task of creating a form that will autofill with the information from a table. The information the form autofills is selected using a primary key called ModID. I have a combo box that has a List of the ModIDs that are listed as Active.
SELECT ModID
FROM P_Review
WHERE Status = "Active"
Simple enough. I then have VBA code running on the event After Update. So after the value for the combo box is select or changed it will run this VBA code.
Option Compare Database
Option Explicit
Private Sub selectModID_AfterUpdate()
'Find the record that matches the control.
On Error GoTo ProcError
Dim rs As Object
Set rs = Me.RecordsetClone
With rs
.FindFirst "ModID=" & Me.selectModID
If Not .NoMatch Then
Me.Bookmark = .Bookmark
Else
DoCmd.RunCommand acCmdRecordsGoToNew
Me!localModID = Me.selectModID.Column(0)
End If
End With
ExitProc:
Exit Sub
ProcError:
MsgBox "Error: " & Err.Number & ". " & Err.Description
Resume ExitProc
End Sub
The code runs fine (I get no errors when I debug or run).
Now for the access text box. I would like to populate certain fields based off the variable localModID. I have a dlookup in a text box to find the information in the table P_Review.
=DLookUp("Threshold","P_Review","ModID =" & [localModID])
So the DlookUp should find the value for the column threshold, in the table P_Review, where the ModID in P_Review equals the localModID set in the VBA code. But when I go to form view and select a ModID I get the Error 3070: The Microsoft Access database engine does not recognize as a valid field name or expression. I did copy this code from another database we are already using but it fails in this new instance.
Private Sub ModID_AfterUpdate()
Dim rs As Object
Set rs = Me.RecordsetClone
With rs
.FindFirst "ModID='" & Me.ModID & "'"
If Not .NoMatch Then
Me.Bookmark = .Bookmark
Else
DoCmd.GoToRecord , , acNewRec
Me!ModID = Me.ModID
End If
End With
End Sub
This is the answer to question. I used this code to auto update.
Try
Forms!<whatever_this_form_name_is>![localModID]
in your DLOOKUP