open form record with button - ms-access

I need to open a form (shipments) from a button on another form and have that opened form be on that "CustomerID"s record so you can easily add a new shipment to that customer. Any suggestions? I have tried using a macro to no avail but code eludes me. everything is linked via customerID under relationships.
I am using access 2007.

Create a new button called btnOpenForm, and add this to the module:
Private Sub btnOpenForm_Click()
On Error GoTo Err_btnOpenForm_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "frmMyForm"
stLinkCriteria = "[CustomerID] = '" & Me.CustomerID & "'"
DoCmd.OpenForm stDocName, , , stLinkCriteria
Exit_btnOpenForm_Click:
Exit Sub
Err_btnOpenForm_Click:
MsgBox Err.Description
Resume Exit_btnOpenForm_Click
End Sub
The key here is the variable 'stLinkCriteria'. You set it to include the field name ("CustomerID") and the value you want to filter by ("Me.CustomerID"). If either your table's field name or your textbox name are different, you'll have to edit those values appropriately. Also, you'll have to change the value of 'stDocName' to whatever your form's name is as well.

Related

Setting a item in a forms property from another forms vba

I have a form which has a button that has a click procedure.
Essentially, if a checkbox(chkIncludeIDs) is ticked then it has to open a form and make the property of a textbox on that form visible.
My current code for the click event is as follows:
Private Sub Update_pending_Click()
On Error GoTo Err_Update_pending_Click
Dim stDocName As String
Dim stLinkCriteria As String
If Me.chkIncludeIDs = 0 Then
stDocName = "Pending Queries Locked"
Else
stDocName = "Pending Queries Locked"
Forms![Pending Queries Locked]!ID.Visible = True
End If
DoCmd.OpenForm stDocName, , , stLinkCriteria
Exit_Update_pending_Click:
Exit Sub
Err_Update_pending_Click:
MsgBox Err.Description
Resume Exit_Update_pending_Click
End Sub
The form that I open is "Pending Queries Locked"
The textbox I need making visible is ID as it's default value is visible = false.
My code isn't working.
please name your controls without spaces like "form_pending_queries_locked", query_my_query, txt_id, lbl_id for many good reasons.
when you access a control's property you need to specify which property you are accessing.
e.g
If Me.chkIncludeIDs.value <> 0 Then
regarding your question. You need to open the form first before accessing its controls. so that would be logically: open the form, access the control, change its property.
in code it would be
Dim txt_id As Textbox
DoCmd.OpenForm "frm_myform"
Set txt_id = Forms![frm_myform]!txt_id
txt_id.visible = False
or alternatively: you can send a parameter via OpenArg and access it via on form_load event which will then change local controls properties..

Open a form to a specific tab

When a button is pressed I want to open a form at a specific tab.
on click event I have:
DoCmd.OpenForm "MAIN_USER_FORM", acNormal, , "App_Ref = " & Me.App_Ref, , , "PRB"
On the open event of the form I have:
If Me.OpenArgs = "PRB" Then
Me.PRB_Validation.SetFocus
End If
PRB_Validation is the name of the tab in the MAIN_USER_FORM I wish to open.
I've searched forms, I just can't get it to work any help would be great.
Thanks in advance.
All you need is to check the OpenArgs in the form's OnLoad event, and set the TabCtontrol's value to the index of the page you want to show, like this:
Private Sub Form_Load()
If OpenArgs = "PRB" Then
TabCtl0.Value = PagePRB.PageIndex
End If
End Sub
I made an example accdb to show the complete setup.
In case if anyone is looking for code where you have a button on another form and want to open Main form from that and also take user to a particular Tab.
Private Sub YourButton_Click()
On Error GoTo Err_YourButton_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "YourFormName"
DoCmd.OpenForm stDocName, , , stLinkCriteria
Forms![YourFormName]!YourPage.SetFocus
Exit_YourButton_Click:
Exit Sub
Err_YourButton_Click:
MsgBox Err.Description
Resume Exit_YourButton_Click
End Sub

form tabcontrol subform runtime error 468: Object doesn't support this property or method

I am developing a search form / report builder in Access 2007 called frmSearch. The existing search form works well, showing its results in a subform of a tabcontrol. Then I can click a button to show a report of all tests, which works fine. I want to modify the code to show a report for a single fldTestsID, but am stuck with the Form/Subform/Control path syntax.
There are two tabs: tabResultsTabular shows a subform frmResultsTabular as a query-like list of row. tabResultsRecord shows a subform frmResultsRecords, showing a single record. The report is rpt_TestDatasetExposureEffects. The report's underlying query is q_TestDatasetExposureEffect containing a field called fldTestsID.
So, the path is frmSearch to tabResultsRecord containing button cmdQAReportResults to frmResultsRecords to fldTestsID.
I see that that are other posts for the same error, but did not get them to work. The Access 2007 docs on DoCmd.OpenReport do not mention this specific instance.
Here is the code of the cmdQAReportResults click event including the options I have tried. It fails on the line strRptWhere =. The DoCmd.OpenReport syntax is working based on the Access 2007 docs.
Private Sub cmdQAReportResults_Click()
doQAReport
End Sub
Private Sub doQAReport()
'On Error GoTo Err_doQAReport 'comment during debugging
Dim stDocName As String ' report name
' Dim strRptSQL As String ' report SQL String
Dim strRptWhere As String ' report WHERE clause
stDocName = "rpt_TestDatasetExposureEffects"
'Override the recordsource to match the current record TestsID
' strRptSQL = "SELECT * FROM q_TestDatasetExposureEffects WHERE fldTestsID = " & fldTestsID
' DoCmd.OpenReport stDocName, acPreview
strRptWhere = "0 = 0"
strRptWhere = "fldTestsID = " & Me.Form![tabResultsRecord].fldTestsID.Value 'error 468
' other attempts follow
' strRptWhere = "fldTestsID = " & Forms("frmSearch").Controls("tabResultsRecord").Form.Controls("frmResultsRecords").Form.Controls("fldTestsID").Value
' strRptWhere = "fldTestsID = " & Me.Form.fldTestsID 'error 2465
DoCmd.OpenReport stDocName, acPreview, , strRptWhere
' Reports("rpt_TestDatasetExposureEffects").RecordSource = strRptSQL
Exit_doQAReport:
Exit Sub
Err_doQAReport:
MsgBox Err.Description
Resume Exit_doQAReport
End Sub
The correct syntax is:
Forms(<Parent Form>).<control container for subform>.form.<control>
In your case this might be:
Forms("frmSearch").frmResultsRecords.Form.fldTestsID
frmResultsRecords is the name of the subform, but is it also the name of the subform's container?
If not, replace it with the name of the control on the main form containing the subform.

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

Filter a form using a command button on another form

I have a form with a cmdbutton that at the moment opens another form and shows all records for several types of PartitionStyles and TrimFinishs (486 at present), I need to be able to filter the second form to show only the TrimFinish I need.
Private Sub lbl600SeriesS_Click()
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "frmModules"
stLinkCriteria = "Forms!frmModules![TrimFinish] = 1"
DoCmd.OpenForm stDocName, , , stLinkCriteria
End Sub
At the moment it shows only a new record, I know there should be 162 records using 1, what have I missed or done incorrect.
Base stLinkCriteria on a field in frmModules' RecordSource. So, if the RecordSource includes a numeric field named TrimFinish, try something like this:
stLinkCriteria = "[TrimFinish] = 1"
If the RecordSource is a query drawing from more than one table, you can qualify the field name with the table alias:
stLinkCriteria = "YourTableAlias.[TrimFinish] = 1"
If you still have trouble, edit your question to describe frmModules' RecordSource. If it's a query, paste in the SQL View of the query.