Open a form to a specific tab - ms-access

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

Related

How to clear search bar text when closing form

I have a MainMenu with a search bar and two buttons. Button1 opens MainForm, Subform1, and Subform2 in VIEW only mode, filtered to the SchoolID typed into the unbound search bar.
My question is: When the user returns to the MainMenu by clicking my CloseFormOpenMainMenu button , how can I clear what is typed into the MainMenu search bar and clear the filter?
Here is my code on the MainMenu:
Private Sub Button1_Click()
Dim txtSearchBar As String
Dim Cancel As Integer
On Error GoTo ErrorBEDSIDSearch
DoCmd.OpenForm "MainForm", , , "SchoolID = " & ("""" & Me.txtSearchBar.Value
& """"), acFormReadOnly
Exit Sub
End Sub
Here is my code on the MainForm:
Private Sub CloseFormOpenMainMenu_Click()
DoCmd.Close
DoCmd.OpenForm "frmMainMenu"
End Sub
If it helps, I have the "Filter On Load" property = No and the "Filter" property is blank.
You can use the following code to clear the textbox from the other form:
Forms("MainMenu").txtSearchBar.Value = Null
Implemented:
Private Sub CloseFormOpenMainMenu_Click()
DoCmd.Close
DoCmd.OpenForm "frmMainMenu"
Forms("MainMenu").txtSearchBar.Value = Null
End Sub

open form record with button

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.

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..

Opening a zoom window in MS Access

I am having an issue with opening a zoom window in a sub form.
Basically, I have created a pop up window (form) which is suppose to appear upon double clicking in a memo field in a sub form to allow the user to be able to zoom in on the field and have additional ease upon entering long sentences.
I believe the problem is linked to the fact that the form which I am trying to create the zoom window is actually a sub form embedded in a form. My reasoning behind this is due to the fact that my code works perfectly well when i open the sub form alone and double click on the zoom in field..
Below is the code. The subform name is 'frmMasterListOfEventsDetails", the control / field to zoom in on in the sub form is called "notes2". The pop up window (subform) is named "frmZoom" and its control (text box) where the information is to be entered is called "txtZoom".
I would appreciate any help you may have.
Thank you
Private Sub Notes2_DblClick(Cancel As Integer)
If Me.AllowEdits = False Then
Messaggi.MessaggioExclamation
Else
Me.Refresh
DoCmd.OpenForm "frmzoom", acNormal, , , , acDialog
End If
End Sub
Private Sub Form_Close()
Forms("frmMasterListOfEventsDetails")!Notes2 = Me.txtZoom
Forms("frmMasterListOfEventsDetails").Refresh
End Sub
Private Sub Form_Open(Cancel As Integer)
Me.txtZoom = Forms("frmMasterListOfEventsDetails")!Notes2
End Sub
I believe the problem is linked to the fact that the form which I am trying to create the zoom window is actually a sub form embedded in a form
I believe you are correct. Since frmMasterListOfEventsDetails is a subform,
Forms("frmMasterListOfEventsDetails")
will not find it. You need to go through the main form:
Forms("parentFormName").Form.frmMasterListOfEventsDetails.Form.Notes2 = Me.txtZoom
I'm late but you can just use the SHIFT+F2 to get a zoom a any textbox in Microsoft Access
Probably, a better aproach (since you can reuse it on other forms) is this:
Private Sub Notes2_DblClick(Cancel As Integer)
If Me.AllowEdits = False Then
Messaggi.MessaggioExclamation
Else
Me.Refresh
DoCmd.OpenForm "frmzoom", acNormal, , , , acDialog, Me!Notes2
if IsOpened("frmzoom") then
Me!Notes2 = Forms!frmzoom!txtZoom
DoCmd.Close acForm, "frmzoom"
end if
End If
End Sub
'Independent module
Public Function IsOpened (stNameOfForm as string) As Boolean
Dim i As Integer
For i = 0 To Forms.count - 1
If Forms(i).Name = stNameOfForm Then
IsOpened = True
Exit Function
End If
Next
End Function
'frmzoom module
Private Sub Form_Open(Cancel As Integer)
Me.txtZoom = Me.OpenArgs
End Sub
Private Sub Btn_OK_Click() 'frmzoom Button OK
Me.Visible = False
End Sub
Private Sub Btn_Cancel_Click() 'frmzoom Button Cancel
DoCmd.Close
End Sub
As you can see, the zoom dialog receives the data from the form that calls it and then collects the information directly depending on the status (open / closed); thus the Zoom dialog does not need to know the name of any control and can be used by any form.

runtime error 2448 you cant assign a value to this object

I am using David-W-Fenton's answer to this question to try to allow users to print a report when they click on a control, but I am getting the following error message:
runtime error 2448: you cannot assign a value to this object.
The line of code triggering the error is:
Me.txtPageTo.Value = numPages
Here is the full code of the OnLoad event handler for the form:
Private Sub Form_Load()
Dim varPrinter As Printer
Dim strRowsource As String
Dim strReport As String
If Len(Me.OpenArgs) > 0 Then
strReport = Me.OpenArgs
Me.Tag = strReport
For Each varPrinter In Application.Printers
strRowsource = strRowsource & "; " & varPrinter.DeviceName
Next varPrinter
Me!cmbPrinter.RowSource = Mid(strRowsource, 3)
' first check to see that the report is still open
If (1 = SysCmd(acSysCmdGetObjectState, acReport, strReport)) Then
With Reports(strReport).Printer
Me!cmbPrinter = .DeviceName
Me!optLayout = .Orientation
End With
Dim numPages As String
numPages = Reports(strReport).Pages
Debug.Print "numPages = " & numPages
TypeName(Me.txtPageTo) 'added this line as a test after
Me.txtPageTo.Value = numPages
End If
End If
End Sub
numPages prints out to equal 0 just before the error is thrown, even though the report should have at least one page. Also, the error does not get thrown when the form is already open. The error only gets thrown when the form has to be opened. (Probably because the offending code is in the onload event.)
When I added TypeName(Me.txtPageTo) , it triggered runtime error 2424: The expression you entered has a field, control, or property name that mydatabasename cant find.
I think the problem is that I need to set the control source for txtPageFrom and txtPageTo. But how do I do that? This form will only be loaded by a vba method that is trying to print a report, so the values for txtPageFrom and txtPageTo will come from the calling method, not from some underlying data table. (there is no underlying data table for this dialog form.)
cmbPrinter and optLayout seem to be populating correctly using the code above.
Can anyone show me how to get past the error messages so that the form loads properly?
CORRECT, COMPLETE ANSWER:
The answer to this problem was to greatly simplify the code. The code in the link at the top of this posting is WAY TOO COMPLEX, and tries to reinvent things that are already done well by the tools built into Access and VBA. I was able to print reports without any of the complicated solutions by simply using the following code in the on-click event of a control on a form associated with the report:
Private Sub txtPrintReport_Click()
On Error GoTo Error_Handler
Dim commNum As Long
commNum = Me.CommunicationNumber
Dim strReport As String
Dim strVarName As String
strReport = "rptCommunicationFormForPrinting"
strVarName = "CommunicationNumber"
DoCmd.OpenReport strReport, acViewPreview, , strVarName & " = " & commNum, acWindowNormal, acHidden
DoCmd.RunCommand acCmdPrint
DoCmd.Close acReport, strReport
Exit_Point:
Exit Sub
Error_Handler: 'this handles the case where user clicks cancel button
If Err.Number <> 2501 Then
MsgBox Err.Description, _
vbExclamation, "Error " & Err.Number
End If
DoCmd.Close acReport, strReport
End Sub
That is all the code that was required. Much less than the link at the start of this question. And also much less than the answer below. I am marking John Bingham's answer as the accepted answer because he clearly worked a lot on this, and I am very grateful for that. But I ended up solving the problem with A LOT LESS CODE. My solution does not require the custom dialog form because it uses the Windows print dialog form. As such, the on load event code in my posting (taken from the link above), is not necessary.
The code posted cannot work, because when a form is opened as a dialog:
DoCmd.OpenForm "dlgPrinter", , , , , acDialog, strReport
no other logic after this line is executed until the form has been closed, at which point control returns to the next line after this one, and logic continues on - except of course you can no longer reference anything in that form, because it is now closed.
Ok, now there is a question, where is the button the user clicks to print a report?
What I'm thinking is to split the logic in PrintReport into two methods, one that launches it, and tells the form to configure itself (doing the stuff suggested in the comment), but the rest of PrintReport then needs to happen after the user clicks OK (or Cancel);
So if I assume that you've got a form which can launch one or more reports, and the button is on this form, what I would suggest is this:
In the click event for that button - no changes to what you've got.
In that buttons form, add this:
Public Sub DialogAccept()
With Forms!dlgPrinter
If .Tag <> "Cancel" Then
Set Reports(strReport).Printer = Application.Printers((!cmbPrinter))
Reports(strReport).Printer.Orientation = !optLayout
Application.Echo False
DoCmd.SelectObject acReport, strReport
DoCmd.PrintOut acPages, !txtPageFrom, !txtPageTo
PrintReport = True
End If
End With
DoCmd.Close acForm, "dlgPrinter"
DoCmd.Close acReport, strReport
Application.Echo True
End Sub
Change PrintReport to:
Public Function PrintReport(strReport As String, strVarName As String, numVal As Long) As Boolean
' open report in PREVIEW mode but HIDDEN
DoCmd.OpenReport strReport, acViewPreview, , strVarName & " = " & numVal, acHidden
'DoCmd.OpenReport strReport, acViewPreview, , , acHidden
' open the dialog form to let the user choose printing options
DoCmd.OpenForm "dlgPrinter", , , , , , strReport
Forms!dlgPrinter.Configure
End Function
In the OK/Cancel button click events on dlgPrinter put (after existing code, but removing any instances of "Docmd.close"):
Forms!Calling_Form_Name.DialogAccept
This then calls that method, to do the stuff that is meant to happen after the user says "I'm done with the dialog".
Finally add the configure method to dlgPrinter:
Public Sub Configure()
With Reports(Me.Tag).Printer
Me!cmbPrinter = .DeviceName
Me!optLayout = .Orientation
End With
Dim numPages As String
numPages = Reports(Me.Tag).Pages
Debug.Print "numPages = " & numPages
TypeName(Me.txtPageTo) 'added this line as a test after
Me.txtPageTo.Value = numPages
End Sub
And remove this code section from Form_Load.
Hoep this helps.
Lastly, if the form on which the button launching dlgPrinter can vary, change this line:
DoCmd.OpenForm "dlgPrinter", , , , , acDialog, strReport
to:
DoCmd.OpenForm "dlgPrinter", , , , , acDialog, strReport & ";" & me.Name
In form_load of dlgPrinter, break up me.Openargs using left() & mid() with instr(), and save me.Name into the tag of something on the form (which you're not already using the tag of).
In the OK/Cancel button click events, change the code above to:
Forms(Object.Tag).DialogAccept