I have the following lines of code in 2 different VBA functions in a Module they both have the same aim of opening a form to a specific record;
stLinkCriteria = "[ID]=" & Reports![Rpt_Manufacture].[ID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
and
stLinkCriteria = "[ID]=" & Forms![frmManufactureList]![frm_Products].[ID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
How can I change this so I only have one function that I can call from reports or forms and it will open the form to a specific record. I have tried the me! version on reports but I get an 'Invalid use of Me keyword' which I guess is because I can not use it from a Module.
UPDATE #1
Based on the answer below by Thomas G I used this code;
Option Compare Database
Public Function CmdOpenProductDetails(ByRef theObject As Object)
On Error GoTo Err_CmdOpenProductDetails
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "FrmProductDetails"
stLinkCriteria = "[ProductID]=" & theObject![ProductID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
Exit_CmdOpenProductDetails:
Exit Function
Err_CmdOpenProductDetails:
MsgBox Err.Description
Resume Exit_CmdOpenProductDetails
End Function
And from the Form and Report for the product names I have an onClick event that reads;
=CmdOpenProductDetails()
However, if I click a product name on a form I get the error message;
The express On Click you entered as the event property setting
produced the following error. The expression you entered has a
function contain the wrong number of arguments. (The expression may
not result in the name of a macro, UDF or Event Proc) (There may have
been an error evaluating the function).
If I click from a report I get the error message;
MS Access cannot find the object 'CmdOpenProductDetails(). Make sure
you have saved it and that you have typed it correctly.
Pass the form byref in a sub
SOmething like
Public Sub Open_Form(ByRef theForm As Form)
Dim stLinkCriteria As String
stLinkCriteria = "[ID]=" & theForm![ID]
DoCmd.OpenForm theForm, , , stLinkCriteria
End Sub
Private Sub TestIt()
Open_Form Forms![frmManufactureList]![frm_Products]
End Sub
Note that you might have to tweak it a bit because I dont know the exact context. Maybe you have a subform and so you should pass the mainform instead
But the idea is there
UPDATE following Erik proposal:
You can pass either a form or a report as an object to make it work for both
Public Sub Open_Form_or_Report(ByRef theObject As Object)
Dim stLinkCriteria As String
stLinkCriteria = "[ID]=" & theObject![ID]
If TypeOf theObject Is Form Then
DoCmd.OpenForm theObject , , , stLinkCriteria
ElseIf TypeOf theObject Is Report Then
DoCmd.OpenReport theObject , , , stLinkCriteria
Else
MsgBox "Error : the type should be a Form or a Report"
End
End Sub
Private Sub TestIt()
Open_Form_or_Report Forms![frmManufactureList]![frm_Products]
End Sub
Related
I have a button that runs a macro that calls function "CheckRecordCount()" which then calls Sub Command1_Click.
I want a pop up if the form does not proceed to a new blank record (in other words a new record is not inserted).
I tried an error function but the error seems to execute regardless.
Public Function CheckRecordCount()
Call Command1_Click
End Function
Private Sub Command1_Click()
On Error GoTo FailedInsertion
DoCmd.GoToRecord , , acNewRec
FailedInsertion:
MsgBox "Fill out all fields and try again. Record insertion failed."
End Sub
I solved my problem.
Public Function CheckRecordCount()
Call Command1_Click
End Function
Private Sub Command1_Click()
Dim varTotalBefore As Integer
Dim varTotalAfter As Integer
varTotalBefore = Forms![your form title].Form.Recordset.RecordCount
On Error Resume Next
DoCmd.GoToRecord , , acNewRec
On Error GoTo 0
varTotalAfter = Forms![your form title].Form.Recordset.RecordCount
If varTotalAfter = varTotalBefore Then
MsgBox "Request submission failed. Ensure all fields are filled out and try again."
End If
If varTotalAfter > varTotalBefore Then
MsgBox "Request successfully submitted!"
End If
End Sub
I am currently getting "Run-time error '-2146500594 (800f000e)': Method 'Form' of object'_SubForm' failed" when I run the following code:
TempVars.Add "TempFlightID", Me![FlightsPlan_SubFrm].Form!Text48
I am working on a set of navigation buttons for a set of forms some with and some without subforms in MS-Access. The goal is to have it open the needed form and have it be in the same record in the parent form as on the form that the user is currently on, and if there is a subform the same record in the subform as well. If there is not a subform it should save the ID for the record and the next time the user opens a form with a subform it should open with that ID.
I think I have the parent form navigation functional using open arguments, but can't seem to get the subform to work as intended. I am attempting to accomplish this by saving the subform ID as a TempVars as below (objects renamed for clarity):
Private Sub OpenParentForm2Btn_Click()
IDref = Me.ID
TempVars.Add "TempID", Me![SubFrm_1].Form![SubFrm_1ID]
DoCmd.OpenForm "ParentForm2", , , , , , IDref
DoCmd.Close acForm, "ParentForm1"
End Sub
Then on the second subform:
Private Sub Form_Load()
If [TempVars]("TempID") > "" Then
Me.Recordset.FindFirst "ID=" & [TempVars]("TempID")
Else
DoCmd.GoToRecord , , acLast
End If
End Sub
and for completeness here is the working code on the second parent form:
Private Sub Form_Load()
If Me.OpenArgs > "" Then
Me.Recordset.FindFirst "ID=" & Me.OpenArgs
Else
DoCmd.GoToRecord , , acLast
End If
End Sub
Any help resolving the error or directing me to a different method to accomplish this than TempVars would be appreciated.
-Cameron
I have a check box on my access form. If the user selects that check box , one column in an access report should display the values in 1000's
How can this be done?
By passing the value of the checkbox into the OpenArgs when you run the report, you can set the format of the column. Here's what the report opening code might look like:
Private Sub cmdRun_Click()
Dim bFormatted As Boolean
bFormatted = chkFormat.Value
DoCmd.OpenReport "ReportName", acViewPreview, , , , bFormatted
End Sub
Then in the Open event of the Report:
Private Sub Report_Open(Cancel As Integer)
Dim sArgs As String
Dim bFormatted As Boolean
sArgs = OpenArgs & ""
If sArgs <> "" Then
bFormatted = CBool(sArgs)
End If
If bFormatted Then
txtBox.Format = "0000"
End If
End Sub
I have not checked the correct format. That's left up to you.
I've manipulated and tried a different way about going about something. I have a combo-box(Coding_drop_down) on a form(Coding Pop Up).
I previously had taken over maintenance for this database from someone else so everything was pre-existing. The combo-box itself had a Value List in it and referenced that when clicking the drop down arrow. However I've since changed that and bound it to a table that I created (for something past this). Now when I go to the drop down I get those specific values that I did before only loaded from a table rather than a Value List. The problem is that when selecting the value it does not select. Am I missing something or does the VBA need to be manipulated ???
VBA Code for the combo-box:
Private Sub Command1_click()
On Error GoTo Err_Command1_Click
Dim stDocName As String
stDocName = "Query to do easier coding"
DoCmd.OpenQuery stDocName, acNormal, acEdit
Exit_Command1_Click:
Exit Sub
Err_Command1_Click:
MsgBox Err.Description
Resume Exit_Command1_Click
End Sub
The code for the Button :
Private Sub Command7_Click()
On Error GoTo Err_Command7_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "Query to do easier coding"
DoCmd.Close acForm, stDocName
DoCmd.OpenForm stDocName, , , stLinkCriteria
Exit_Command7_Click:
Exit Sub
And the on Click event of the actual Combo-box:
Private Sub Coding_drop_down_Click()
Dim test As String
test = Me.Coding_drop_down
MsgBox test
End Sub
Ok so try this out for your comboBox it might give you a good start. The items you want in the comboxBox are listed in the .addItem and they are added to the ComboBox once the form is started. When you select the item it then stores the value in the first cell of the sheet you want. If this is something that can help you out let me know and we can work towards helping with your problem.
Private Sub UserForm_Initialize()
With ComboBox1
.AddItem ("Item 1")
.AddItem ("Item 2")
.AddItem ("Item 3")
End With
End Sub
Private Sub ComboBox1_Click()
ActiveSheet.Range("A1") = ComboBox1.Value
End Sub
The issue was that the combo-box was actually bound to something and that was the reason for the issue.
I have a form with a subform that are linked by an ID. The problem is the subform has a combo box that needs a parameter from the main form for the row source.
The form is about sales calls and they need to be able to link many proposals and contracts as they want. The combo box for the list of proposals and contracts needs to be in relation with the mills or clients of this sales call.
I know I could do a Forms!FormName!ControlName to get the mill list but does this mean I have a design problem?
Or should I do a list filled when a user click on an element in a combo box in the main form? but then I would have to handle the save and delete myself.
Thank you
If a subform requires a main form to function properly you can check for a parent. For example:
Private Sub Form_Open(Cancel As Integer)
Dim strParent As String
Dim strSubname As String
GetParent Me, strParent, strSubname
If strParent = "None" Then
MsgBox "This is not a stand-alone form.", , "Form Open Error"
Cancel = True
End If
End Sub
Function GetParent(frm, ByRef strParent, ByRef strSubname)
Dim ctl
On Error Resume Next
strParent = frm.Parent.Name
If Err.Number = 2452 Then
Err.Clear
strParent = "None"
Else
For Each ctl In frm.Parent.Controls
If ctl.ControlType = acSubform Then
If ctl.SourceObject = frm.Name Then
strSubname = ctl.Name
End If
End If
Next
End If
End Function