I am making a data entry MS Access based database. It has a main screen, where I enter the person ID, and once I click Create, it directs to a navigation form which displays 4 other forms (basically 4 pages of data entries for 1 person).
This form also displays the person_ID and date in the header. When I enter the person ID and click Create, I should be able to enter the details for that respective record. But when the form opens it redirects to the first record in the DB and not that particular record. Kindly suggest me the proper way to do it.
To open a form on a particular record you can use DoCmd.OpenForm method with a WhereCondition.
On the first form have a text box for the person_ID.
This will be referred to as me!person_ID in your code.
DoCmd.OpenForm "Form2", acNormal, , "person_ID = " & me!person_ID
That's it.
For something a little more advanced you can use the forms Form.OpenArgs property.
The code below does not allow the form to be opened unless an OpenArgs is supplied.
That may not be necessary but It prevents users from opening the form through a route I do not wish them to take.
Then the code behind the "Create" button will be something like:
DoCmd.OpenForm "Form2", acNormal, , , , acNormal, me!person_ID
On the form you wish to open on the specific record on ( Form2 ) use the following code in the "On Open" event:
Private Sub Form2_Open(Cancel As Integer)
If IsNull(Me.OpenArgs) Then
MsgBox "Open from the first form, I won't work otherwise"
DoCmd.Close , "Form2"
Cancel = True
Exit Sub
End If
Me.Filter = "person_ID = """ & Me.OpenArgs & """"
Me.FilterOn = True
End Sub
The filter is now set in the second form's "On Open" event rather than being supplied by the WhereCondition on the first forms Create buttons "On Click" event.
As a design choice have you thought about using a single form and having a "Tab Control" with a tab in place of each of your forms. Instead of the Create button opening the next form it simply changes tab.
This is my first post on stackoverflow, did I do good?
Related
I am trying to open a form to a specific record from another form using VBA in Access 2016. I am doing it with the DoCmd.openForm but I don't know why it is not working. Is it because of Access 2016 or am I missing something?
Here is my sample code:
Docmd.openForm "Add_contacts",,,"frmEntryContacID=" & Me.contac_ID
As you can see in the picture, it opens the Add_Contacts form but it doesn't display any details information about the contact.
The contac_ID field is where I want to click and open the second form which will give me more details about the contact. So basically the code is going behind the click event of contac_ID. I hope that does not pose a problem.
Help guys please.
Is "frmEntryContactID" your field name? Also is Me.contact_ID a numeric value?
You have two options to open a form with specific record using Docmd.OpenForm
1. DoCmd.OpenForm "Add_contacts", , , "Contac_ID=" & Me.Contac_ID which #Erik has already mentioned. Make sure Add_contacts form is bound to Contacs table and Allow Filters property is set to Yes.
2. Use the OpenArgs property to pass the parameter value to new form.
DoCmd.OpenForm "Add_contacts", , ,, , , Me.Contac_ID
And add below code to Add_Contacts form
Private Sub Form_Current()
If Not IsNull(Me.OpenArgs) Then
Me.RecordSource = "select * from Contacs Where Contac_ID=" & Me.OpenArgs
End If
End Sub
Could you please help me regarding this coding issue.
I have 3 forms as follows:
1) frmAuditProceduresMain – A main form which includes data that will be used later.
2) frmAuditProceduresSummarySubFrom – which is the subform of frmAuditProceduresMain mentioned above. This form includes the button which triggers the code.
3) frmAuditProceduresDetails – A separate standalone form that opens on pressing the button in frmAuditProceduresSummarySubFrom.
Both frmAuditProceduresSummarySubFrom and frmAuditProceduresDetails includes a field called ProcedureName.
Initially I open frmAuditProceduresMain & through it I right the data in the ProcedureName field of its subform (whose name is frmAuditProceduresSummarySubFrom) then I press the button that is located next to this record.
When the button is pressed frmAuditProceduresDetails should open and as it is the first time to add that record, the ProcedureName that I have just added should be added automatically in the ProcedureName field of frmAuditProceduresDetails along with another field from frmAuditProceduresSummarySubFrom called ProcedureID in addition to getting data from fields of the main form (frmAuditProceduresMain).
But if the records already exist, I need when I press the button, I can view/edit that record specifically on the frmAuditProceduresDetails.
I wrote the following code, but it returns all the field of frmAuditProceduresDetails as empty and I do not know why:
Public Sub cmdAuditProceduresDetails_Click()
DoCmd.Save
Form.Refresh
DoCmd.OpenForm "frmAuditProceduresDetails", acNormal, , , acFormAdd, acWindowNormal
If Forms!frmAuditProceduresDetails!ProcedureName <> Me.ProcedureName Then
Forms!frmAuditProceduresDetails!ProcedureName = Me.ProcedureName
Forms!frmAuditProceduresDetails!ProcedureID = Me.ProcedureID
Forms!frmAuditProceduresDetails!AuCode = Me.Parent!AuCode
Forms!frmAuditProceduresDetails!AuName = Me.Parent!AuName
Forms!frmAuditProceduresDetails!AuditCycle = Me.Parent!AuditCycle
DoCmd.RunCommand acCmdRefresh
Else
DoCmd.OpenForm "frmAuditProceduresDetails", , , "ProcedureName = '" & Nz(Me.ProcedureName, "") & "'"
End If
End Sub
I shall be grateful if anyone can help me regarding it.
Regards.
Raya.
I form that contains a subform that lists multiple records as a datasheet. In the subform is a checkbox type column called "Select". (control name is chk_select).
This column is used to select which records they would like to open in a separate form. After checking which records they would like to open by checking the checkboxes, a button then opens a separate detail form to the records where the "Select" column = True.
My issues is that unless the focus is on a line in the datasheet where the checkbox is selected, Access can't open the detail form.
For example, I would check the box to select the record I wanted to open, then click on a different line where the checkbox is not selected, and try to open the detail form but Access doesn't seem to see that there are records selected. But if I move the cursor to any line where the checkbox is checked, then the detail form opens just fine for all the records where the checkbox is selected.
My current workaround is to display a message box to try and get the user to move the cursor to a line where the checkbox is selected. But I'd like to have this done automatically before opening the other form to avoid any confusion for the user.
So I need a way to use SetFocus to a subform control where the checkbox = True before opening the detail form.
Is there any way to do this or a workaround?
Here is what I have so far.
Thank you.
Private Sub btn_open_estimate2_Click()
If Forms![View Estimates]![TblEstimates subform]![chk_select] = True Then
'Forms![View Estimates]![TblEstimates subform]![chk_select].SetFocus ***Need something similar to Where [chk_select]=True
DoCmd.OpenForm "Edit Estimate", , , "TblEstimates.select = True"
DoCmd.Close acForm, "View Estimates", acSaveYes
Else
MsgBox "Please select an Estimate to open, or " & _
"move the cursor to a selected Estimate to be able to open it", VbMsgBoxStyle.vbExclamation, "No Estimate Selected"
End If
End Sub
Use the RecordsetClone:
Private Sub btn_open_estimate2_Click()
Dim rs As DAO.Recordset
Set rs = Me![TblEstimates subform].Form.RecordsetClone
rs. FindFirst "[select] = True"
If rs.NoMatch Then
MsgBox "Please select an Estimate to open.", VbMsgBoxStyle.vbExclamation, "No Estimate Selected"
Else
DoCmd.OpenForm "Edit Estimate", , , "TblEstimates.select = True"
DoCmd.Close acForm, Me.Name, acSaveYes
End If
rs.Close
End Sub
As a different method to validate your checkbox. This may solve your problem.
If Forms![View Estimates]![TblEstimates subform]![chk_select] = True Then
To
If Not IsNull(DLookup("[Edit Estimates]", "[TblEstimates]", "[Select] = True")) Then
I have a login form:
Each button decides what source will be used by cbUserLogin ("Nazwa" on screenshot)
CODE:
Private Sub buttonUAP1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Forms!MainLoginForm!cbUserLogin.RowSourceType = "Table/Query"
Forms!MainLoginForm!cbUserLogin.RowSource = "queryUsersUAP1"
End Sub
And for each button the same code with the different query source.
I want to do something to actually decide where user will be directed based on that button.
If while logging in UAP 1 button is selected and login and password will match I want to land user in form_UAP1, if button UAP 2 will be selected while logging in I want to make user land on form_UAP2, etc.
I'm trying to bite that thing for already some days and I just can't find solution.
For now, while login and password match with query this happens (ofc. it works):
If Me.txtPassword.Value = DLookup("haslo", "dbUzytkownicy", "[Identyfikator]=" & Me.cbUserLogin.Value) Then
compIdentyfikator = Me.cbUserLogin.Value
'Zamykam formularz logowania i przenoszę do elementu docelowego
Me.Visible = False
DoCmd.OpenForm "test", acNormal, , , acFormEdit, acWindowNormal
Else
MsgBox "Hasło które zostało podane jest nieprawodłowe", vbOKOnly, "Złe dane!"
Me.txtPassword.SetFocus
End If
I'm using "hide" instead of cmd.close because I'm trying to pass compIdentyfikator to the other forms (they will be used later)
But 1st I want to bite that.
Perhaps you can use a combobox control instead of a set of buttons. You could even use radio buttons to accomplish the same idea (if I'm understanding what you want). Direct your user to Select UAP, then when they click Login, you can read the UAP selection from the combobox or radiobutton group.
Create a Frame with Radiobuttons (named frmUAP in my example):
Then in your form's code, set up the Click event of the Frame group of option buttons:
Option Compare Database
Option Explicit
Private Sub fraUAP_Click()
'--- reload the combobox based on the user selection
Me.cbUserLogin.RowSource = "queryUsersUAP" & Me.fraUAP.Value
Me.cbUserLogin.Requery
End Sub
Private Sub cmdZaloguj_Click()
If (all_user_input_fields_have_been_validated) Then
DoCmd.OpenForm "frmUAP" & Me.fraUAP.Value
End If
End Sub
Your login Combobox should be refreshed every time the user selects a different Radiobutton.
NOTE: make sure the Radiobuttons (option buttons) each have the correct integer value in the group... optUAP1 value = 1, etc
I have a report with details of jobs/tasks, and also a form which contributes the majority of the data towards that report. Given that a report is a nice way of looking at the larger picture of the data, and a form is the best way of editing data, I would like to be able to click on a row, and have it open up the relevant record in the form view.
Does anyone know how to do this through VBA? In my mind it should be possible, though my knowledge of objects in Access is limited.
Update
I've implemented the following code for my report:
Private Sub Edit_Click()
Dim EntityName As String
Dim DocName As String
DocName = "Entity: Overview"
strWhere = "[Entity Name]='" & Entity & "'"
DoCmd.OpenForm DocName, acNormal, , EntityName
End Sub
It successfully opens the correct form, and it also grabs the correct entity name, however it isn't filtering properly. I can't see what the issue is with the code.
In your Edit_Click() procedure, you have EntityName as the WhereCondition parameter to OpenForm.
DoCmd.OpenForm DocName, acNormal, , EntityName
However, you haven't assigned anything to EntityName, so it's an empty string. I think you should use strWhere as the WhereCondition.
Private Sub Edit_Click()
Dim strWhere As String
Dim DocName As String
DocName = "Entity: Overview"
strWhere = "[Entity Name]='" & Me.Entity & "'"
DoCmd.OpenForm DocName, acNormal, , strWhere
End Sub
I assumed Entity is the name of a control, and that's where you get a value to build strWhere. If there is no control in the report by the name of Entity, then the code won't work even if there is an Entity in the original query.
You should be able to add an On Click event in the Detail section of the report, then add something like this in the VBA handler:
DoCmd.OpenForm "MyForm", acNormal, "", "ID=" & ID.Text
(where MyForm is the target form, and ID is a hidden or visible control in your report that identifies the current record).
You say report, but you should not be using a report but a continuous form or datasheet. You can then add events to any of the controls on any line, and add a double-click event, if you do not want to drive your users insane. You could also add a command button if that would be clearer to your users, or format the "link" textbox to underline. The record opened by the action shown by #dbaseman will open which even record has the focus (current record). And that will lead you to discover what you can and can't do with a continuous form :D