Microsoft Access - Record Form Load - ms-access

I am curious whether it is possible that when i load a record in one of my forms and i choose to add a quote for that record selected, so i choose add quote (button) which takes me to my quote page. i would then like the form to auto load the record i had previously selected in the other form.
Client Form:
Quote Form:
Here is the data flow:
Record Selected ("Add Job") > Click "Add Items" button > "Items List" loads > the record i previously selected in "Add Job" is then auto loaded.
the feild that will need loading are "Project ID" & "Client name"

Use OpenArgs argument of the DoCmd.OpenForm method
When you click Add Quotes button to open Quotes form, send the details/linked ID via openArgs parameter.
In Quotes form load event, you can get the details passed using Me.OpenArgs
See below sample code
On Add Quotes button click
Private Sub AddQuotes_Click()
DoCmd.OpenForm "frmQuotes", OpenArgs:=me.ClientID
End Sub
Quotes form
Private Sub Form_Load()
Dim varArgs
varArgs = Me.OpenArgs
'Fill the controls with recordset data
If Not IsNull(varArgs) Then
With CurrentDb.OpenRecordset("SELECT * FROM tblClients WHERE ClientID = " & varArgs, dbOpenForwardOnly )
Me.ClientID = !ClientID
Me.ClientName = !ClientName
Me.ClientAddress = !ClientAddress
Me.ClientPhone = !ClientPhone
Me.ClientEmail = !ClientEmail
.Close
End With
End If
End Sub

Related

MS Access Combobox not taking a value after requery shows value

I have Combobox that lets users either edit the related values for this field
- cboBEA using a button. I decided to use a NotInList routine whenever they want to ADD a new value. The edit part works without a hitch, but the NotInList part needs to accept a value after providing related fields in a popup form called frmBEA_JDIR
At first the requery of cboBEA wasn't working, so I did a more deliberate resetting of the rowsource by first setting it to "" then the actual SQL of the rowsource.
Here is the code in the "Save" button of the popup form frmBEA_JDR
Private Sub cmdSave_Click()
Select Case Me.OpenArgs
Case "Edit"
DoCmd.Save
DoCmd.Close acForm, "frmBEA_JDIR"
With Form_sfm_AddSPDistro
.cboBEA.Requery
.cboBESA.Requery
.cmbPROGRAM.RowSource = ""
.cmbPROGRAM.RowSource = "SELECT * FROM qLU_BEA_JDIR;"
.cmbPROGRAM = .cboBEA
End With
Case "AddNew"
Dim strSQL As String
strSQL = "SELECT LU_BEA_JDIR.ID, LU_BEA_JDIR.BEA, LU_BEA_JDIR.BESA, LU_BEA_JDIR.ORGANIZATION " _
& "FROM LU_BEA_JDIR;"
With Form_sfm_AddSPDistro
'cboBEA.Requery doesn't work, so...
.cboBEA.RowSource = ""
.cboBEA.RowSource = strSQL
.cboBESA.Requery
.cboBEA.Value = Me.txtBEA
.cmbPROGRAM.RowSource = ""
.cmbPROGRAM.RowSource = "SELECT * FROM qLU_BEA_JDIR;"
.cmbPROGRAM = .cboBEA
End With
DoCmd.Close acForm, "frmBEA_JDIR"
End Select
End Sub
Here is the NotInList event of the calling form:
Private Sub cboBEA_NotInList(NewData As String, Response As Integer)
Dim MsgBoxAnswer As Variant
Response = acDataErrContinue
Me!cboBEA.Undo 'Used this to prevent the requery error caused by frmBEA_JDIR
MsgBoxAnswer = MsgBox(NewData & " is not in the list. Do you want to add it?", vbQuestion + vbYesNo, "Add " & NewData & "?")
If MsgBoxAnswer = vbNo Then
Me.cboBEA = Null
DoCmd.GoToControl "cboBEA"
Else
DoCmd.OpenForm "frmBEA_JDIR", acNormal, , , acFormAdd, , "AddNew"
Form_frmBEA_JDIR.txtBEA = NewData
End If
End Sub
So depending on what calls this form - the NotInList or the Edit, I put it in the openargs parameter that calls frmBEA_JDIR. This is how I handle the update in the SAVE button. Again, the edit part works perfectly, but the AddNew from the NotInList event just won't populate cboBEA even after it is requeried and I can see the new value in it.
In brief: Instead of figuring out what to do in the popup with openargs, let acFormAdd do it for you; acFormAdd will open the form to a new record.
Send the new data in openargs.
Open frmBEA_JDIR in dialog mode. It stops the current code until the opened form is closed.
' open frmBEA_JDIR in dialog mode to add the new data.
DoCmd.OpenForm "frmBEA_JDIR", acNormal, , , acFormAdd, acDialog, "NewData"
'data is added, now requery the dropdown
cboBEA.Requery
Fool around with this without the 'case edit' section until you see it working. Then add the edit part using acFormEdit. You can check the datamode of the popped-up form to see if it's add or edit.

Trying to open a form using DoCmd.OpenForm based on sub form control

I have an unbound form with an unbound control (Text0). In Text0 I enter a RefNo. I want to open another form (frmDisclosure) which has a subform (frmRefNosList) containing the RefNo. My code for opening frmDisclosure is:
Private Sub Command8_Click()
Dim Refce As Variant
DoCmd.OpenForm "frmDisclosure"
Forms!frmDisclosure.FilterOn = False
Refce = "Forms!frmDisclosure!frmRefNosList.Form!RefNo"
DoCmd.OpenForm "frmDisclosure", acNormal, "", Refce & " = " & Me.Text0, , acNormal
End Sub
After our discussion in chat, I mocked-up something that hopefully looks like what you were trying to do:
Link to Access file mock-up.
To others that might read this later here are a few clarifications:
Form Form1 has a textbox called Text0 on it and a button.
When the button is pressed it is supposed to open Form2 that has a
subform on it.
Form2 is then supposed to open to a certain record and then filter the
records shown on its subform all using value provided in Text0
Here's what my mock-up of Form1 looks like:
So when I provide a RefNo and click the button...
... the following code (annotated) is run on the click event of that button:
Private Sub cmdOpen_Click()
' check user has provided a RefNo in the textbox...
If _
Me.Text0 = "" Or _
IsNull(Me.Text0) _
Then
' ...if a RefNo has not been provided, give an error message
MsgBox "Please provide a Ref No.", vbExclamation Or vbOKOnly, "Disclosure Search"
Else
' ...if a RefNo has been provided, look in the table that lists an applicant's
' forms and return the ApplicantID (or equivalent field you're using) for the RefNo provided
Dim varApplicantID As Variant
varApplicantID = DLookup("ApplicantID", "tblRefNos", "RefNo=" & Me.Text0)
' ...check that an applicant record can be actually be found from the RefNo provided
If _
IsNull(varApplicantID) _
Then
'... if an applicant record is not found, give an error message
MsgBox "Could not find an applicant with the RefNo provided.", vbExclamation Or vbOKOnly, "Disclosure Search"
Else
'... if an applicant record is found, open Form2 to that applicant's record
DoCmd.OpenForm "Form2", , , "ApplicantID=" & varApplicantID
'... and then filter the subform on Form2 by the RefNo provided
Forms!Form2!frmApplicantForms_sub.Form.Filter = "RefNo=" & Me.Text0
Forms!Form2!frmApplicantForms_sub.Form.FilterOn = True
Forms!Form2!frmApplicantForms_sub.Form.Requery
End If
End If
End Sub
This should result in Form2 opening to the correct applicant record derived from the RefNo provided and also filter the subform by the RefNo provided:
Your exact project layout may differ to what I've mocked-up, so some of this may need to be tweaked for your set up, but hopefully the principles I've illustrated here will be easy enough to translate.
Good luck!
This was not the answer I wanted but: I created a query that yields the ID number of frmDisclosure corresponding to Text0 on the unbound form. I then retreive that ID number using DLookup from within the unbound form and open frmDisclosure that way. It would have been nicer to do it all from within the unbound form.

how to filter on load in MS access?

when double clicking on a value i need to open a form. the value gets passed to the opened form. I want the form to directly filter on that value instead of pushing on a button first. i tried filtering on change and on load but it doesn't work. when loading it doesn't know the value because it gets added after it opened the form.
this is the code for passing the value:
DoCmd.OpenForm "SubmenuRubrieken", acNormal
Forms!SubmenuRubrieken.Tv_rubrieknaam.Value = Me.Tekst14.Value
this is the code for filtering on that value in Tv_rubrieknaam:
Dim filter As String
filter = ""
If Not IsNull(Tv_rubrieknaam) Then filter = filter & " AND rubrieknaam = '" & Tv_rubrieknaam.Value & "'"
Me.filter = Right(filter, Len(filter) - 5)
Me.FilterOn = True
for some reason it doesn't trigger the filter on changing the value of Tv_rubrieknaam. how do i need to solve this?
I guess, the Form_Load() event is finished before you set the value (OpenForm is performed before value is set), so you would have to do the filtering in the OnChange() event of the Textfield or similar.
Better: pass the Me.Text14.Value with the DoCmd.OpenForm command either as a prepared whereCondition OR as OpenArgs with filtering option in the On_Load event.
A basic example:
I have a Form1 with a TextBox called Text0 on it. Text0 has a value of 2.
I have a Form2 with a Table called Table1 as Recordsource. Table1 has a column called Field1 containing numbers between 1 and 3
All I need to do is add the following code into the module of the Form1 and the moment I click on Text0 Form2 will be openend filtered down to rows with Field1 = 2
Private Sub Text0_Click()
DoCmd.OpenForm "Form2", acFormDS, , "Field1 = " & Nz(Me!Text0, 0)
End Sub

Function to determine and pass value in a form control

I am trying to create a function that will determine the value held in an active form control and then pass the value for a form criteria as follows:
Public Function fc_id() As Long
fc_id = (Screen.ActiveControl.Caption)
End Function
Public Sub sShowForm()
DoCmd.OpenForm "F_Details", , , "c_id = " & fc_id
End Sub
I then call the function from the form control being clicked:
sShowForm
The details form opens but it's black, and there is not error message.
If I use a numeric value, what would be the equivalence of the
(Screen.ActiveControl.Caption)
Temporarily change your procedure to find out what you're getting from Screen.ActiveControl:
Public Sub sShowForm()
'DoCmd.OpenForm "F_Details", , , "c_id = " & fc_id
MsgBox Screen.ActiveControl.Name
End Sub
You may not be getting what you expect. For example, if you're using a command button to run sShowForm(), the command button is the ActiveControl. Whether or not that is the situation, check to see what you're actually getting.
If the present form (the form where your VBA code is running) includes a text box bound to a c_id field in the form's record source, you can eliminate the function and reference that text box directly in the OpenForm option. For example, if c_id is a numeric field and the text box is named txtc_id ...
DoCmd.OpenForm "F_Details", , , "c_id = " & Me.txtc_id
If c_id is text type, add quotes around the text box value ...
DoCmd.OpenForm "F_Details", , , "c_id = '" & Me.txtc_id & "'"

Form transition and data refresh

I have a form with a button and when I push it, it opens another form. When I close this second form I need to refresh the first form.
So I need to insert the first form name in a global variable to access the form name from the second form to refresh the first form.
I tried create this:
global formname as form
formname = activeform.name
but I receive an error saying: The option is read only.
On the Click event of the button you can pass the name of the form as the OpenArgs property of DoCmd.OpenForm.
Private Sub Command4_Click()
DoCmd.OpenForm "frmStaff", , , , , , Me.Name
End Sub
On the Close event of the second form you can check this (string) value:
Private Sub Form_Close()
If Me.OpenArgs <> "" Then
'MsgBox Me.OpenArgs
Forms(Me.OpenArgs).Refresh 'or .Requery
End If
End Sub
Why don't you just go into the VBA code behind and refresh it with the close sub? I don't understand the need for a global variable to be passed between the forms - You should be able to just do something like this in your second form:
Private Sub Form_Close()
Form_YourFormHere.Refresh
End Sub
If you must use this approach, try this in the first form's module:
Public formname as Form
Private Sub Command1_Click()
Set formname = Me
End Sub
or
Public formname as String
Private Sub Command1_Click()
formname = Me.name
End Sub
A form is an object type, and can only be assigned to a variable using a Set statement, otherwise VBA will think that you are trying to assign the value of the object to the variable.
You said that you wanted the form name, however your variable was a Form type, not a String. The first code snippet will achieve the result you appeared to be trying to achieve, and the second will achieve the result you said you wanted to achieve.
A different approach would probably be better and more reliable, though.