5 replies
by timfox
Chances are that you are using a form that is bound to a recordset. (Most are, but if not, let us know as answer could be different).
Use this code within the OnClick procedure of your form's Save/OK/Close button:
Dim db As Database
Dim rst As Recordset
Set db = CurrentDb
DoCmd.RunCommand acCmdSaveRecord
If IsNumeric(txtRecordNum) Then
Set rst = db.OpenRecordset("SELECT RecordNum FROM tblThisTable WHERE RecordNum = " & txtRecordNum)
If rst.RecordCount = 1 Then DoCmd.OpenReport "rptThisReport", acViewPreview, , "RecordNum = " & txtRecordNum
Else
MsgBox "ERROR - Record not added to DB", vbCritical, "Notify Administrator"
End If
End If
rst.Clone
db.Close
Note
1. This code forms the basis of what you could do, but changes will need to be made because your field names, text box names and recordsets etc will be different.
2. The DoCmd.RunCommand acCmdSaveRecord may not be needed.
Hope it Helps
TimFox
by pc_tek
by timfox
Additional thoughts...
Ensure that your report's FilterOn property is set to True.
The code above does not use a global variable, although doing so is another way of solving your problem. To use a global variable, you would have to ensure the report also references the global variable in either it's query or the filter property.
Assigning the RecordNum value to a global variable is done by
1. Setting up and writing a method to return the global variable
Create a module with the following contents:
Option Compare Database
Option Explicit
Public gblRecordnum As Integer
Public Function GetRecordNum() As Integer
If IsNumeric(gblRecordnum) Then
GetRecordNum = gblRecordnum
Else
GetRecordNum = -1
End If
End Function
---------------
2. Change your On_Click to this:
Dim db As Database
Dim rst As Recordset
Set db = CurrentDb
DoCmd.RunCommand acCmdSaveRecord
If IsNumeric(txtRecordNum) Then
Set rst = db.OpenRecordset("SELECT RecordNum FROM tblThisTable WHERE RecordNum = " & txtRecordNum)
If rst.RecordCount = 1 Then
gblRecordnum = txtRecordNum
DoCmd.OpenReport "rptThisReport", acViewPreview
Else
MsgBox "ERROR - Record not added", vbCritical, "Notify Administrator"
End If
End If
rst.Clone
db.Close
---------------
3. Place this code in the report's On_Open procedure:
If GetRecordNum > 0 Then
Me.Filter = "RecordNum = " & GetRecordNum
Else ' function returned -1
MsgBox "ERROR - GetRecordNum", vbCritical
End If
---------------
Good Luck
TimFox
by pc_tek
by pc_tek
This discussion is closed to new replies.
Trending Web Development posts
Sort by Trending