Access 2007 Setting Field to Selection in Dialog - ms-access

I would like to set a field to a value selected from a grid in a dialog. I am using Access 2007.
In WinForms I would:
Create a child form
create a grid for the data
add a property for the selected item
In the parent form
add a button to open the form
on successful dialog result get the selected item from the property
update the object
persist on event
On parent edit
set selected value in child grid
Is something like this possible in Access 2007 forms? I have a Multiple Item form with the child records. Can I select one and return that to the parent? And on the other side, can I default the selected item on edit?
How do people approach this in Access?

Here is a pattern that works assuming the child form is modal.
In your parent form
Private Sub cmdOpenChild_Click()
DoCmd.OpenForm "ChildDialog", acNormal, , , , acDialog, "Info for child"
'This line will block further code execution until child form is hidden or closed.
MsgBox Forms.Item("ChildDialog").Controls.Item("SomePropertyOrControl").Value
DoCmd.Close acForm, "ChildDialog"
end sub
In the Child form have a close button that actually only hides the form.
Private Sub cmdClose_Click()
'hide the form instead of closing it to return control to caller.
Me.Visible = False
End sub

Related

how to create a combo box in access which can be converted to text box to enable writing if the user needs to add a new option to the existings?

I was wondering how I can define a combo box in a form where the user can select an option from the existing list, and if he can't find the desired option, a "add a new option" can be accessed which converts the combo box to a text box for writing the new option?
You can put a button next to the combo, for an 'add option' which is triggered when the combo 'not in list' event is hit - and the button then would call a popup form - or in your suggestion, changes the visibility of an overlayed text box that allows for entry. I suggest a popup form, however, because then you can validate the input for length, content, etc - and then once the form or text box is filled in - exiting the text box, or submitting the form would save to your table for the drop down, refresh the combo box source query, and assign the value. I do this in VBA alot with popups.
Sorry my bad english :-(
You can directly use the "On Not in List" event. Look at the examples at
https://learn.microsoft.com/en-us/office/vba/api/Access.ComboBox.NotInList
This is the scenario: A combobox called cb and a textbox called tb. Visible property of tb is false. Combobox and Textbox have the same data source. When you type a value that is not in the combobox list, that value goes to the textbox; when you finished editing textbox, it is hidden again.
Private Sub cb_NotInList(NewData As String, Response As Integer)
' Suppress error message and undo changes.
Response = acDataErrContinue
Me!cb.Undo
' Prompt user to verify they wish to add new value.
If MsgBox("Value is not in list. Add it?", vbOKCancel) = vbOK Then
Me!tb.Visible = True
Me!tb.Value = NewData
Me!tb.SetFocus
End If
End Sub
Private Sub tb_AfterUpdate()
' Hide again texbox
Me!tb.Visible = False
' Update combobox items
Me!cb.Requery
End Sub

Toggle visibility of other fields on form based upon combobox selection - MS Access

Question...
How can I toggle the visibility of several other fields (checkboxes/textboxes) on form based upon the selection of a combobox item. The image below shows a listbox but either way, how do use vba code to turn on or off visibility of all the fields in the grey box. Basically, if the combobox selection is scheduled then visible=true. Else visible=false How can I code this???
Use combobox AfterUpdate event and probably form Current event as well. So build a procedure that can be called from both events, something like:
Sub Form_Current()
SetVisible
End Sub
Sub cbo1_AfterUpdate()
SetVisible
End Sub
Sub SetVisible()
Me.tbx1.Visible = Me.cbo1 = "scheduled"
Me.cbx1.Visible = Me.cbo1 = "scheduled"
End Sub
Alternative is to use Conditional Formatting for textboxes and comboboxes (sorry, not applicable to other controls) to Enable/Disable as well as set colors so appear not visible.

MS-Access: Populate modal form from custom type

I am trying to display an error form, populated with text from a custom type object, as a MS-Access version of the fantastic answer on this Excel question.
The problem I run into is that I want to wait for a user response/confirmation on this error form, so it must be (as far as I am aware) a modal form and therefore I cannot just open the form and immediately populate it.
I have tried to find a way of doing in Access what was done in Excel; load the form, populate the form then display the form, but this doesn't seem possible since Access' event order is Open->Load->...
I have also tried looking for a way to open as a normal form, populate and then 'modalise' the form but could not find a way to do this.
Does anyone know of a way to achieve this function? Or is there an alternative to modal forms to pause execution awaiting user input?
A modal form is VERY different from a dialog form.
Modal forms do NOT cause the calling code to halt, but a dialog form does.
(do not confuse the two types of forms).
To “get back” a user response from a dialog form, simply set the form visible = false in place of a close form command. This will KICK the dialog form out of dialog mode, and your calling code now continues and the calling code is “free” to examine any values the user typed in or say choose from combo boxes, or check boxes, or whatever.
So your code block will look like this:
Private Sub Command0_Click()
Dim f As String
Dim strT As String
f = "formB"
DoCmd.OpenForm f, , , , , acDialog
If CurrentProject.AllForms(f).IsLoaded Then
' grab any values entered by user, or whatever buttons hit
strT = Forms(f).Text1
' etc. etc. etc. - grab values from that form
' don't forget to close the form
DoCmd.Close acForm, f
Else
' user canceled the form
' code goes here for user hitting your cancel button or "x"
End If
End Sub
And in your dialog form, the “ok” button, or “close” button simply goes:
Me.visible = False
If the user hits your cancel button, that code would be:
Docmd.Close acForm
So if the user hits “cancel” or even the “X” in the upper right corner, you consider and assume the user “bailed out”. When they do this, the form will be closed.
So the code after the dialog part simply checks if the form is STILL open (because your “ok” button does a visible = false).
And since the form is STILL open, then you are free to grab the values of ANY control – say user text typed into a text box, values from a combo box, check box, or whatever.
When user is done the calling code is free to examine or grab any value(s) from that form.
Edit - 2nd solution to allow "setting" of values.
This code will work:
Private Sub Command0_Click()
Dim f As String
Dim strT As String
f = "formB"
DoCmd.OpenForm f
Forms(f).Text1 = "Hello" ' set values on form
' now WAIT for user
Do While CurrentProject.AllForms(f).IsLoaded
If Forms(f).ok = True Then Exit Do
DoEvents
Loop
If CurrentProject.AllForms(f).IsLoaded Then
If Forms(f).ok = True Then
MsgBox "value return = " & Forms(f).Text1
Else
MsgBox "user cancel"
End If
DoCmd.Close acForm, f
Else
' cancel code goes here
MsgBox "user cancel"
End If
End Sub
The code for the OK button on the form B is now:
Me.Visible = False
Me.ok = True
And you need a public var "ok" in form B.
eg this:
Option Compare Database
Option Explicit
Public ok As Boolean
So the cancel button in form B can simply close the form. (you can close the form - but don't set ok = True)

Grab and return Item from one form to other in Access VBA

Since it's the first time I do this, I want to know the best approach or right way to get the result.
I have a form (Quote) where I have a List Box to add items. I also have another form (Search Item) where I can find all the items in my database. Where I want to do is to grab an item from the Search Item box and put it in my Quote Form List Box.
I was thinking to put a Global Variable that will store the ID of the item and use this as pin point reference to paste in the Quote List Box.
Is this the right way to it or am I missing something here?
To reference the form you're currently on:
Me
To reference a textbox control in the form you're currently on:
Me.txtbox_name_here
To reference a form from another form:
Dim frm As Form
'first you need to open the form to reference it
DoCmd.OpenForm "form name here", acNormal
'references the form the form
Set frm = Forms("form name here")
'when you're done remember to close the form with this
DoCmd.Close acForm, "form name here", acSaveYes
To reference another form's textbox control, using the frm variable I made earlier:
frm.txtbox_name_here
This should be enough information for you to get started.

VBA get previous active control from parent form

I am usgin Access 2010 and I have a form that has a sub form, let's call it sform. I want to print the parent form's active control When I hit a button in sform. I tried
debug.print Me.parent.ActiveControl
But it prints the name of the sub form's container control, while I wanted the one that was active before it. Is that even possible?
Thanks
The active control on the parent form would be the sub form. To figure out what control had the focus before the sub form you may have to do something like the following:
Add a text box to the form called txtFieldThatHadLastFocus or whatever and hide it.
Then in the On Lost Focus event of all your controls on the parent form add:
Private Sub txtMyField_LostFocus()
txtFieldThatHadLastFocus.Value = "txtMyField"
End Sub
Then within the sub form you can figure out what control had focus before the sub form via:
Forms!ParentFromName!txtFieldThatHadLastFocus.value