Access one form open at a time - ms-access

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.

Related

How to close a form onclick in Access

In Access 2007, I have a button that opens up a form in dialog mode (modal and popup set to true). I'm trying to save the data from that form and then close that form like so:
Private Sub Close_Click()
DoCmd.Close acForm, Me.Form, acSaveYes
End Sub
The purpose is to add a new record to a table. It accomplishes this, new data is added to the database based on the inputs. However, the form still stay visible. I have also tried this not using dialog mode, and the result has been the same.
You need to pass DoCmd.Close the form name, not the actual form object.
Private Sub Close_Click()
DoCmd.Close acForm, Me.Name, acSaveYes
End Sub
Also, while you're making changes, add Option Explicit add the top of your module. This would've made it way easier to spot the bug. Read here why having Option Explicit on is a good idea.

Access VBA open form on current record

I have a form in loading records in continuous mode that looks like a datasheet. I want to have a button at the beginning of each row of data that I can click on the will open up a form for users to edit.
So far I got the edit form to load by using
DoCmd.OpenForm
This loads the first record no matter which record's button I click on. I tried the
DoCmd.Gotorecord
and this does not work. How can I fix this>?
DoCmd.OpenForm "YourFormName", acNormal, , "YourUniqueID=" & Me!YourUniqueID

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.

Pausing code to passing Value of User input

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.

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.