Reference Calling Form from a Module - ms-access

I have a function within a module that opens my Home Page Form:
Function goto_home()
Dim stDocName As String
'DoCmd.Close acForm, "CallingForm.Name" , acSaveYes
stDocName = "home_page"
DoCmd.OpenForm stDocName
End Function
It's in a module because I have command buttons on all my forms that use the code instead of duplicating it within each Form Object.
The commented line in my code should close the calling form before the home page is opened. If I was doing it from a form object I would use me.form.name but you can't do that in a module.
Is there an equivalent module syntax that allows reference to the Calling Form?

You could always pass the formname in the function e.g:
Function goto_home(frmName As String)
Dim stDocName As String
Dim stLinkCriteria As String
DoCmd.Close acForm, frmName , acSaveYes
stDocName = "home_page"
DoCmd.OpenForm stDocName, , , stLinkCriteria
End Function
Then as you are calling this from each form, you can put the form's name in the call argument.

Related

Make custom Wait Popup wait Until activity is done MS Access 2010 - 2016

I have a custom Popup that I call whenever an activity takes more than a second. Example:
PopupMsg("Getting records")
It makes a nice box that just shows the user something is happening, then it quietly disappears when the activity is done.
It works great for anything that only takes about 3 seconds, but beyond that, it disappears and then the user is left with the impression that the activity is finished. I'd like to make it stay up exactly as long as whatever activity is happening, but I've never been successful in determining this. I'd like to make sure all screen calculations are done before the popup disappears.
Here's how I implement my PopupMsg routine
Public Function PopUpMsg(strMsg As String, Optional strTitle As String)
Dim frmWait As New Form_Wait
If strTitle <> "" Then
frmWait.OpenForm strMsg & "...", strTitle
Else
frmWait.OpenForm strMsg & "..."
End If
End Function
Wait (A form called 'Wait' contains the following code)
Option Compare Database
Option Explicit
Public Property Let Message(ByVal MessageText As String)
Me.MessageLabel.Caption = MessageText
Me.Repaint
End Property
Public Property Get Message() As String
Message = Me.MessageLabel.Caption
End Property
Public Property Let Title(ByVal TitleText As String)
Me.Caption = TitleText
End Property
Public Property Get Title() As String
Title = Me.Caption
End Property
Public Function OpenForm(Optional MessageText As Variant, _
Optional TitleText As Variant) As Boolean
If Not IsMissing(MessageText) Then Me.MessageLabel.Caption = MessageText
If Not IsMissing(TitleText) Then Me.Caption = TitleText
Me.Visible = True
Me.Repaint
OpenForm = True
End Function
As you're opening a form through a class instantiation, it doesn't actually persist, and gets removed as soon as Access decides to do garbage collection and sees there's no reference to the form. If you want a form that persists until code execution is done, the best way is to pass back that form:
Public Function PopUpMsg(strMsg As String, Optional strTitle As String) As Object
Set PopUpMsg = New Form_Wait
If strTitle <> "" Then
PopUpMsg.OpenForm strMsg & "...", strTitle
Else
PopUpMsg.OpenForm strMsg & "..."
End If
End Sub
The rest of your code is still valid
You can call it like this:
Dim WaitForm As Object
Set WaitForm = PopupMsg("Getting records")
That way, you're still depending on garbage collection to remove the form, but it will close as soon as the function calling it is done.
You could also just open the form through DoCmd.OpenForm "Wait", reference it through the Forms collection, and close it using DoCmd.Close acForm, "Wait" at the end of your function, but then you'll have to close it actively. Full code for that approach:
Public Function PopUpMsg(strMsg As String, Optional strTitle As String)
DoCmd.OpenForm "Wait"
Dim frmWait As Form
Set frmWait = Forms!Wait
If strTitle <> "" Then
frmWait.OpenForm strMsg & "...", strTitle
Else
frmWait.OpenForm strMsg & "..."
End If
End Sub
Call it: PopupMsg("Getting records")
Close it at the end of execution: DoCmd.Close acForm, "Wait"
If you're not calling DoEvents in your code, there's another alternative:
Open the form using DoCmd.OpenForm "Wait", set it's TimerInterval to 1, and add DoCmd.Close acForm, Me.Name to it's Form_Close event

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.

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

how to autoincrement the default value in texbox

Is there a way to autoincrement default value in text box assuming data type as text without having to create table? i needed this to happen when form loads.
This cannot be done when a form is opened, so you would need to call this Sub (located in a Module) to open the form:
Sub ChangeDefaultValue()
DoCmd.Close acForm, "Form1"
DoCmd.OpenForm "Form1", acDesign, , , , acHidden
Dim f As Form
Set f = Forms("Form1")
Dim dv As Integer
dv = f.Controls!Text0.DefaultValue
f.Controls!Text0.DefaultValue = dv + 1
DoCmd.Close acForm, "Form1", acSaveYes
DoCmd.OpenForm ("Form1")
End Sub
You will need to change the form name "Form1" and TextBox name "Text0" to your names.

Compile time: Syntax error

I am able to add controls to a form when it is loaded. I need these controls to be to be catagorized so the user only can see a few at a time. My first thought was to attached the controls to particular tabs. Not sure why the syntax is off in the following code. Thanks for the help!
Private Sub Enter_Records_Click()
Dim stDocName As String
Dim tabPage As String
Dim tabNum As Integer
Dim i As Integer
Dim passedTabName As String
stDocName = "CLForm"
tabPage = "tabControl.tabPage"
stTabName = stDocName & tabPage
tabNum = 8 'the number of tabs on the "CLForm"
DoCmd.OpenForm stDocName, acDesign
For i = 0 To tabNum - 1
passedTabName = stTabName & i
CreateKeywords (stDocName, passedTabName)
Next i
DoCmd.Close acForm, stDocName, acSaveYes
DoCmd.OpenForm stDocName, acNormal
End Sub
Public Sub CreateKeywords(frmName As String, tabName As String)
Another idea that might be better, simply having buttons that show one category of controls at a time. Would this be easier?
You need to use the Call keyword when calling a method with more than one parameter and using parentheses.
For example, these would work:
Call CreateKeywords(stDocName, passedTabName)
CreateKeywords stDocName, passedTabName
//Example of using parentheses with one parameter
Msgbox (stDocName)
Welcome to the wonderful world of VBA. ;)
It is best be careful with calling arguments and parentheses:
Public Sub CallingSub()
CallingArg = "ABCDEF"
CalledFunc CallingArg
Debug.Print CallingArg ''Result: 987654
CallingArg = "ABCDEF"
CalledFunc (CallingArg) ''Result: ABCDEF
Debug.Print CallingArg
CallingArg = "ABCDEF"
Call CalledFunc(CallingArg)
Debug.Print CallingArg ''Result: 987654
End Sub
Public Function CalledFunc(CalledFuncArg)
CalledFuncArg = "987654"
CalledFunc = "Return"
End Function