MS Access: Resizing dialog form from within module - vba

I am trying to open a form in dialog mode, and then resize it to a desired size using DoCmd.MoveSize from a module. Ideally I would like this all to run from said module.
Currently the form opens, but does not get resized. I am guessing this is either because the form is not yet active when the command is being run or because acDialog in the OpenForm command prevents it from being executed. I think I could get away with it plugging the MoveSize command into the OnlOad event of the form but I would prefer having as much of the functionality in the module instead. Is there some way around this issue? Like maybe there is some kind of method to wait for the form to be active or some way to plug a command into the OnLoad event from the module itself? As you can tell I am not too familiar with vba yet. It's part of a little project I am doing. Appreciate any help.
Some sample code is as follows:
This one does not resize the form:
Option Compare Database
Option Explicit
Public Function OpenFormDialog()
DoCmd.OpenForm "MsgBoxForm", acNormal, , , acFormAdd, acDialog
DoCmd.MoveSize , , , 9500
End Function
This one throws an error because the form is not found:
Option Compare Database
Option Explicit
Public Function OpenFormDialog()
DoCmd.OpenForm "MsgBoxForm", acNormal, , , acFormAdd, acDialog
Forms.("MsgBoxForm").DoCmd.MoveSize , , , 9500
End Function

You could open the form in normal mode, resize and make modal. Just make sure the Modal property on the form is No.
With DoCmd
.OpenForm "MsgBoxForm", acNormal, , , acFormAdd, acNormal
.MoveSize , , , 9500
End With
Forms.MsgBoxForm.Modal = True
Keep in mind, any code below the last (modal) statement will still execute. If you need code execution to stop, your only option is to resize in the Load() event directly.
Personally, I believe the the Load() solution is better and you could pass the various sizes through the OpenArgs parameter.

You should use the docmd.movesize in the form_open event of the MsgBoxForm form.
Additionally, by executing a docmd.openform, the control flow of program will transfer to the opened form and statements after the docmd.openform will not execute until the opened form is closed.
This is the reason MsgBoxForm is not in the forms collection after docmd.openform.

Related

How to set the focus on a text box?

In one database form code generates a new record:
Me.Section(0).Visible = True
DoCmd.GoToRecord , , acNewRec
Me!ExhibitName.SetFocus
The focus is not set on the data textbox ExhibitName. The focus is nowhere to be seen.
In another database, the same code (except the text box is DistributorName) does set the focus on the proper data entry box.
In the one that fails, if I enter Forms!ExhibitForm!ExhibitName.setfocus<Enter> in the Immediate window the focus goes to the proper data entry text box.
If I trap on the instruction after the Me!Exhibit.SetFocus and execute the Me!ExhibitName.setfocus<Enter> command in the immediate window, then continue execution, the focus is correct.
It feels like some indeterminate delay is needed between the 2nd and 3rd command in the above sequence. Either that or I failed to set some control somewhere.
A workaround is moving the focus to another control and then back to the first control. Like this:
DoStuff
Me.Section(0).Visible = True
DoCmd.GoToRecord , , acNewRec
Me.anotherControl.SetFocus
Me.ExhibitName.SetFocus
This did not work:
Me!anotherControl.SetFocus
Me!ExhibitName.SetFocus
This did work:
DoEvents
Me!ExhibitName.SetFocus
Thanks!

I can't find the right way to reference "Parent"

On my Access main form I have a list box control (named "ActionsTaken") that shows a list of actions taken that are associated with the main form record. These are kept in a separate table linked to the main table. With a button I open a subform to add items to the list. After typing in the text I press a "Done" button which closes the subform with a macro. When the subform closes the deactivate event triggers an event procedure that validates the data, writes it to the Actions Table, and (hopefully) requeries the list control on the main form. Everything works if I use an explicit reference to the control on the main form for the requery, But this subform is called from several different main forms, so I want to refer to the control on the main form using "Parent." The syntax I think should work is:
Me.Parent!ActionsTaken.Requery
When the code executes I get a debug interrupt on the above line, and when I reset the code execution I get a pop up box with "There is no field named 'Me.Parent!ActionsTaken' in the current record." The control is definitely there (remember, an explicit reference to it works).
I suspect I don't understand how "Parent" should be referenced. I've found many other syntaxes with various combinations of dot and bang, and with the "Me" left off. Some give me different error messages, but none work.
I've found a few awkward work-arounds, but I'm really curious as to what's wrong.
A subform is a form sitting on another form (this is the 'parent') and opens when that other form (the 'parent') is opened and therefore is not a subform and does not have a parent.
Pass form name to second opened form via OpenArgs:
DoCmd.OpenArgs "secondformname", , , , , , Me.Name
Then the second form can reference the first:
Forms(Me.OpenArgs).ActionsTaken.Requery
Another approach is to open second form with acDialog which will suspend first form code execution until second form closes.
DoCmd.OpenArgs "secondformname", , , , , acDialog
Me.ActionsTaken.Requery
I have it working, thanks to June and others. I bound the "Child" form to a table even though it's not needed as this lets me indirectly pass the record number (ID) via the OpenForm command. I pass the "Parent" form name to the "Child" (Add New Action subform) with the OpenArgs argument of the OpenForm command:
strFormName = Me.Name
DoCmd.OpenForm "Add New Action subform", , _
"Comment Card ID Query", "[Comment Card].ID=[ID]" _
, , , Me.Name
When I am done with the "Child" I execute the following code:
Dim strParentName As String
strParentName = Me.OpenArgs
' Requery the Actions List Box on the Parent
[Forms](strParentName)!ActionsTaken.Requery
' Close the form
DoCmd.Close
I tried June's suggested syntax: Forms(Me.OpenArgs....) but I got errors from Access in Office 365. Using a string variable worked.
Thanks for all the help!

Form button intermittently returns runtime error 2101

I have a simple form on which the user selects a value from a list in a combo box, then clicks a button to open another form filtered by the selection. In development and testing it works fine, but in Prod users are occasionally receiving runtime error 2101: 'The setting you entered isn't valid for this property'. If the user restarts their machine the error doesn't persist, at least for a while.
This happens when the user clicks the 'Ok' button, which closes the selection form and opens the main interface form. The code for the button is just:
Private Sub btnOK_Click()
DoCmd.OpenForm "CC_Tracker_from_form", acNormal, , , acFormEdit
DoCmd.CLOSE acForm, "frmCoord_Selector"
End Sub
When the user closes the error popup, the selection form remains visible on top of the main interface, which sort of makes sense if the failure is in the DoCmd.CLOSE line, since Access would have already opened the main form.
Why would error 2101 trigger only some of the time, when the user performs exactly the same action (even the same selection from the combo box)?
I don't think this error has anything to do with the underlying Record Source for the main form, but just in case here's that code:
SELECT
[Bunch of columns],
IIf(dbo_CC_Tracker.RISK_LVL='Low',Null,dbo_CC_Tracker.CHRA+365) AS CHRA_Next,
IIf(dbo_CC_Tracker.RISK_LVL='High',dbo_CC_Tracker.[ICP/Review]+29,
IIf(dbo_CC_Tracker.RISK_LVL='Medium',dbo_CC_Tracker.[ICP/Review]+89,Null)) AS ICP_Next,
IIf((dbo_CC_Tracker.RISK_LVL='Low' Or dbo_CC_Tracker.RISK_LVL='Medium'),Null,dbo_CC_Tracker.F2F+179) AS F2F_Next,
IIf(dbo_CC_Tracker.RISK_LVL='Low',Null,dbo_CC_Tracker.ICTCont+89) AS ICT_Next,
dbo_CC_Tracker.HTR_Letter +30 AS Final_Follow,
dbo_CC_Tracker.ASSIGNED +59 AS Deadline_1,
dbo_CC_Tracker.ASSIGNED +89 AS Deadline_2
FROM dbo_CC_Tracker
WHERE
(((dbo_CC_Tracker.ASSIGNED_CARE_COORDINATOR)=Forms!frmCoord_Selector!cmbCoords)
And dbo_CC_Tracker.[CLOSE] is null)
Or Forms!frmCoord_Selector!cmbCoords Is Null;
It's possible that for some reason the form you are trying to close hasn't been able to save any updates.
I would make sure that if the form has any data changed it is saved, so maybe add
If Me.Dirty Then Me.Dirty = False
To force a save before closing the existing form.
Based on the idea that the first form may be closing before passing the dropdown selection to the second form I've changed from closing the form, DoCmd.CLOSE acForm, "frmCoord_Selector", to hiding the form, Me.Visible = False which should keep the selection active despite the first form no longer displaying.

MS Access will occasionally remove checkboxes from my Yes/No fields and replace them with 0 and -1

My new database is built using a variety of queries and tables. These tables and queries have two checkbox fields that are set for "Test" and "Approved". The way that these come into play is that I have buttons on one form called "ApprovalForm" that open up another form called "ViewForm". The goal was that "ViewForm would change its subform's source object every time one of these buttons is clicked. ApprovalForm closes itself first, then ViewForm opens, then Subform.SourceObject is updated to the query specified by whatever button was clicked.
Private Sub View_NonSize_APL_Click()
On Error GoTo View_NonSize_APL_Click_Err
DoCmd.Close acForm, ("ApprovalForm")
DoCmd.OpenForm ("ViewForm")
Forms!ViewForm!View_Subform.SourceObject = "Query.NonSize - APL View"
Forms!ViewForm!View_Subform.Form.Requery
Forms!ViewForm.Form.Requery
Forms.ViewForm.Caption = "NonSize - APL View"
View_NonSize_APL_Click_Exit:
Exit Sub
View_NonSize_APL_Click_Err:
MsgBox Error$
Resume View_NonSize_APL_Click_Exit
End Sub
With the process finished, the final result should be that the fields are populated by the query, which is looking at a table off on the side. The user has no access to the table itself, just that specific select query. However, in rare occasions, the checkboxes will not load correctly, instead displaying as 0s and -1s. What is peculiar about this is that when you click the button on "ViewForm" that leads back to "ApprovalForm" and click the same exact button AGAIN, the checkboxes load correctly.
My guess is that it has something to do with how "ViewForm" loads in, and due to it being a business project, the computer that it is working on is less than optimal. I tried to remedy the situation by using "Requery" and "Refresh" in the code to hopefully reload the process. I have also tried the cumbersome route of having the form close after being loaded and immediately reloading again. (Open the form, load its query, close the form, then open the form again.) That actually proved to ruin the entire process.
Is there anything I can do to counter this problem?

How to launch a Look up form, and return the value from a combo box

Hopefully I can explain what I want to do well enough...here it goes...
I have a data entry form...the user will be entering employeeIDs. Once in normal operation, most people will be entering only their own EmpID, and they should know it, so this won't be a big problem 99% of the time once this DB goes live.
However, I need some temps to enter historical data from paper sheets into the DB. These people will not know anyone else's EmpID. I'd like to set the Student field's OnDblClick event in the subform's datasheet to open a small form with a combo box. The combo box has a list of all Employee Names, and is bound to the EmpID. Once this user enters the name, and selects the person, I have a button they can click to return to the datasheet.
I can use a function to launch the form, no problem there. But how do I return the EmpID to the field in the datasheet that was double clicked?
When user double clicks in the Student field...I want the next form to appear, and then once they type in the name and select the correct person...and then click Found Them!...I need that bound value to return.
I'd love to say I have code to share right now...but the only code I have is to launch the look up form. I'm brain farting on how to pull the value back down.
The way to do this to launch your little dialog form as “acDialog”. This will cause the calling code to WAIT.
And then the “magic” part is when they click on “Found Them” you do NOT close the popup form, but simply set the form’s visible = false. This has the effect of the calling code that popped up this form that halted to continue (the form is kicked out of dialog mode when you do this). So now your calling code continues.
So your code will look like this:
Dim strF As String ' name of popup form
strF = "frmPopUp"
' open form, wait for user selection
DoCmd.OpenForm strF, , , , , acDialog
' if for is NOT open, then assume user hit cancel buttion
' (you should likly have a cancel button on the form - that cancel buttion will
' execute a docmd.close
If CurrentProject.AllForms(strF).IsLoaded = True Then
' grab the value of thee combbo box
strComboBoxValue = Forms(strF)!NameOfComboBox
DoCmd.Close acForm, strF
End If
As noted, the code behind the Found Them button DOES NOT do a close form, but sets forms visible = false (me.Visible = false), and this trick allows the calling code to continue at which point you can examine any value on the form. Remember to then close the form after you grab the value.
It looks like your data table is in a subform so there is a little more work but it does not have to be as complex as the above solution if you don't want it to be. #Andre451 was close but you need the extra step of identifying the form and subform. For the purpose of demonstration let's call the form Attendance and subform Entry then I'll call the second form LookUp. So the code for your double click in the subform field will of course look something like this :
Private Sub Student_DblClick(Cancel As Integer)
DoCmd.OpenForm "LookUp"
End Sub
You really don't need anything else fancy there. For the button on "LookUp" you will put this:
Private Sub Command2_Click()
Forms![Attendance]![Entry].Form![Student] = Forms!Lookup!Student
DoCmd.Close acForm, "LookUp"
End Sub
And that should get you what you want without any overhead or having to leave any ghosts open.