Pausing code to passing Value of User input - ms-access

I'm using Access 2010 and would like to have a user click a button in Form1 called "Save As" that initiates VBA code, and before the code is done running, have it pause, open up Form2 (where the user enters a "Save As" title, and clicks Save), and then have the code continue running in Form1 using the string entered in Form2 as a variable.
I'm just not sure how to "pause" my code in Form1 as it waits for the user to make an input. Any ideas on how to approach this?

From Form1, open Form2 in dialog mode. Form1 code will wait on Form2.
Add a "done" button to Form2, and add a procedure to its click event which reopens Form2 hidden hides the form by setting its .Visible property to False. Making it hidden breaks it out of dialog mode. So after the user inputs her value, she can click "done", flow control will resume in Form1 and can read the input value from the control on the now-hidden Form2.
With this approach, Form2 need not know anything about Form1. So Form2 can be used to gather input for any arbitrary form ... or other code.
So assuming Form1 has a command button named cmdSaveAs, use something like this as its click event procedure.
Private Sub cmdSaveAs_Click()
DoCmd.OpenForm "Form2", acNormal, WindowMode:=acDialog
' the next line is not executed while Form2 is modal (open in dialog mode);
' but after Form2 is hidden, we can read the value of the text
' box named txtInput
MsgBox "User input was: " & Forms!Form2!txtInput
End Sub

You'll want to open Form2 using Docmd.OpenForm with the WindowMode parameter set to acDialog. This causes code on the calling form (Form1) to stop and wait for the called form (Form2) to close. On Form2, when you click the Save button, you could write the Save As title to a hidden textbox control on Form1 or set a property on Form1 to the value you want. Make sure you plan for what you want to happen if the user doesn't enter a value for the Save As title when you return to Form1.

Related

Access 2007 - LostFocus Event

On a form I have a Command button. After the user clicks the button I want to disable the button so it can't be clicked again. I know that you can't disable a control while it still has the focus. So I created this event procedure:
Sub Command1_LostFocus()
Me.Command1.Enabled = False
End Sub
After I clicked the command button, and then tabbed to another control, I expected the above Sub to run. But I get the error message "You can't disable a control while it has the focus". I'm surprised because I thought that when the event procedure was executed Command1 had lost the focus.
Any suggestions on how I can disable a Command button after clicking it?
Move the focus to another control:
Me!SomeControl.SetFocus
Me!Command1.Enabled = False
And do name your controls something meaningful.

Userfriendly forms for links in MS Access

I'm trying to create a form with the following behavior:
Clicking it when it's empty opens the dialogue window of inserting a hyperlink
Clicking it when there's a hyperlink opens the hyperlink
Clicking a nearby "Clear" button clears the form deleting the hyperlink (if present)
I'm currently stuck with step 2. The code for hyperlink insertion window is this one:
Private Sub PSIC_Click()
Me.[PSIC].SetFocus
On Error GoTo browse_stop
RunCommand acCmdInsertHyperlink
browse_stop:
End Sub
When I try to apply different if then variations it doesn't work as expected. Either I fail in properly applying if then or in determining that the form is empty.
The OnClick() event of your hyperlink textbox would be:
Private Sub PSIC_Click()
On Error GoTo browse_stop
If ISNULL(Me!PSIC) Then
RunCommand acCmdInsertHyperlink
End If
browse_stop:
End Sub
I removed SetFocus because clicking on the textbox should automatically set the focus, but if you need it there for some other reason it wasn't really hurting anything.
The "Clear form" command button's OnClick() event would be:
Private Sub cmdClearForm_Click()
Me!PSIC = NULL
End Sub
To open a hyperlink from a form in MS Access:
In Design View make sure that the hyperlink is in its own text box and then go to the 'property sheet' then half way down you should see the option for Hyperlink Address. In here add the address of the hyperlink.
If you want to click on an image and have it open a hyperlink then right-click on the image go to hyperlink.

Access one form open at a time

What can i do for this?
I want make MS access open only one form at a time. This means i have landing form as main form and when i open another form from button in main form, the main form should close and keep only the form i open. Likewise, when i close this form with close button, it should return back to home form.How can this be done? I have tried using Macro, but macro only allows to open main form but does not close main form when i open another form. Any help would be much appreciated.
You can open the form in dialog mode, which will allow the user to only work in that form until it is closed. Any other forms will remain on the screen behind it though, but the user can not bring them into focus until the dialog form is closed.
So on your main form, you have a button to open the form. In the property sheet, click the event tab. Select the ... and choose "Code Builder". Then edit the on click procedure to look something like:
Private Sub btnOpenMyForm_Click()
On Error GoTo Err_btnOpenMyForm_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "frmMyForm"
DoCmd.OpenForm stDocName, acNormal, , , , acDialog
Exit_btnOpenMyForm_Click:
Exit Sub
Err_btnOpenMyForm_Click:
MsgBox Err.Description
Resume Exit_btnOpenMyForm_Click
End Sub
if you use the button wizard, it will create code very similar to this... you just need to add the acDialog constant to the parameter of the OpenForm method.

Access 2010 VBA: Why can't I reopen a previously closed form?

I have a form "Form1" that opens another form "Form2" when button "Command1" in Form1 is clicked. As shown in the code below, control passes to Form2, and once the user does some stuff with Form2, Form2 either hides or closes itself and then returns control to Form1. Regardless of whether Form2 is hidden or closed, Form1 then does some stuff and closes Form2.
Private Sub Command1_Click()
DoCmd.OpenForm "Form2", , , , , acDialog
If CurrentProject.AllForms("Form2").IsLoaded Then
'Do stuff
End If
DoCmd.Close acDialog, "Form2"
End Sub
This seems to work fine first time that Form1 is opened. However, when I close and reopen Form1 and then repeat the above steps, Form2 does not appear and control passes directly to the If statement in the subroutine above. What's wrong? One clue is that CurrentProject.AllForms("Form2").IsLoaded seems to be permanently set to True after Form2 is opened the first time.
What's going on? How can I fix this? Thanks for any help.
Have you tried?:
DoCmd.Close acForm, "Form2"
The constant acDialog resolves to 3, while acForm resovles to 2. The code is actually telling Access to close a report named Form2. The form is never being unloaded properly, probably, because the wrong constant is going through.

Event not Firing in MS Access VBA

I have a form in MS Access which has an image. The image has an Click event which opens a modal form. The modal form has an OK and Cancel button. When you click the OK button, an event is supposed to fire which tells the main form which button was clicked. (This is to simulate the DialogResult functionality in C#). However, the code in the event handler never runs.
The modal form has the following in the general declarations:
Public Event OnDialogBoxClose(NewRecordID As Long, DialogResult As DialogResults)
and the following code where the OK button is clicked:
RaiseEvent OnDialogBoxClose(NewHardwareBaseItemID, dlgresBtnOKClicked)
The main form has the following in the general declarations:
Dim WithEvents RespondQuickAddClose As Form_qckfrmHardwareBaseItemCreate
and the following event handler:
Private Sub RespondQuickAddClose_OnDialogBoxClose(NewRecordID As Long, DialogResult As DialogResults)
MsgBox "Responding to closing of the dialog box" 'Never happens
Me.Requery
End Sub
Can someone explain why the event handler is never called?
Thanks!
Background:
The purpose of all this is to allow a modal dialog box to add an entry, then return the ID of the entry back to the main form to set the value of controls. For instance, imagine you are filling out an insurance form, and you need to select a brand of car this is not there. You click on an icon which pops up with the modal dialog box to allow you to add the car brand. Then when you click OK, it takes you back to the insurance form and selects the brand of car you just created.
This follows an example I found here:
http://database.itags.org/ms-access-database/80292/
You're making your life way too complicated by applying concepts from a different development environment to Access VBA. While VBA does support WithEvents/RaiseEvent, there's no reason to get that complicated here.
The usual way to work with dialogs in Access is that instead of closing them, you hide them. This allows the code after the form was open to run while leaving the values in the form available for use in that code.
Sample code in the OnOpen event of a report that opens a form for collecting values to filter the report:
Private Sub Report_Open(Cancel As Integer)
DoCmd.OpenForm "dlgDateRange", , , , , acDialog, "ThisYear"
If IsLoaded("dlgDateRange") Then
With Forms!dlgDateRange
If .Tag = "Cancel" Then
Cancel = True
Else
Me.Filter = "[InvoiceDate] Between #" & !txtStart & "# AND #" & !txtEnd & "#"
Me.FilterOn = True
Me!lblDateRange.Caption = StrConv(Trim(("from " + varZLStoNull(Format(!txtStart, "mm/dd/yyyy"))) _
& (" to " + varZLStoNull(Format(!txtEnd, "mm/dd/yyyy")))), vbProperCase)
End If
End With
DoCmd.Close acForm, "dlgDateRange"
End If
End Sub
The dialog form has two command buttons, CONTINUE>> and CANCEL. The CANCEL button sets the form's tag to "Cancel" and sets the form's .Visible property to False. The CONTINUE>> button does nothing but set the form's .Visible property to False. Clicking either of those buttons allows the code to continue on the line after the form is open with the acDialog switch.
My philosophy is to make the dialogs as stupid as possible. The calling code has to know what it's looking for in the forms (i.e., you need to know the names of the controls you're reading data out of), but that could be gotten around by adding customer properties to the form. But then you have to know the property names, so you've just moved the ball.
I have also implemented this kind of thing by wrapping the dailog form in a class module, and then the calling context simply initializes an instance of the class and then pulls the values out of it at the appropriate time. But that's actually more complicated that the approach above.
well I don't agree to
"While VBA does support WithEvents/RaiseEvent, there's no reason to
get that complicated here."
I have worked on various VB6 and VBA project. Recently I coded VBA in excel where I raised an event from winform. Few things to be considered when doing so.
If you are calling non-modal winform in VBA with
withevents/raiseevent. It should work normally as expected. No major
workaround is needed
If you are calling modal winform in VBA. Withevents/raiseevents may
not function as per requirement. A quick workaround is to transfer data using public variables declared in the module file.
You will need to use the workaround and I believe it will work absolutely fine.