I am using access to create a form.
i was wondering how to i get the current record id when i click a button.
I tried "Me.CurrentRecord" in the vb button code. but this done not work.
Can anyone help ?
Private Sub save_record_Enter()
Me.CurrentRecord
End Sub
You can refer to fields in the underlying recordset and form controls in MS Access by name, for example:
Me.ID
MsgBox Me.ID
Me.txtID
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
I have a Project to be done in MS Access 2016, and stubled across an Issue, that should be easy to resolve, however, I have no clue on how to do it.
The Database I am developing is based on a huge, unfiltered datasheet exported by another database. I have a main form headview in which I placed two subforms listview an detailview. The listviewis sorted by a combobox.
Now to what "should" happen: If you click on an entry of said listview, the detailview shows additional information of the clicked entry.
Both subforms are based on the same datasheet. So I went ahead and tried to match them via primary key entries. However, that didnt work. The detailview subform is still empty. I also tried to write a vba macro for the listview with listview.click() that didnt work either.
Is there a way to connect those two subforms within a main form? If so, how do I do that?
I am greatfull for any response,
Have a nice day
-Ninsa
Ok, finally I got the reason for error 2455, it's a timing problem.
When the procedure Form_Current of the listview form is called the first time, the detail subform is not yet bound to the detail subform control, which causes the error.
Possible solutions
Option1: Ignore error 2455
Either add On Error Resume Next in the top of the Form_Current procedure or rewrite it to handle that specific error 2455:
Private Sub Form_Current()
On Error GoTo Catch
With Me.Parent.DetailSubformControl.Form
.Filter = "[ID] = '" & Me.ID.Value & "'"
.FilterOn = True
End With
Finally:
Exit Sub
Catch:
If Err.Number = 2455 Then Resume Finally
MsgBox Err.Number & "(" & Err.Description & ") occured.", vbExclamation, "Attention"
Resume Finally
End Sub
Option2: Control the source objects of the subform controls
Clear the Source Object property of the subform controls in the head form and set them explicit when loading the head form.
That prevents the unlucky timing at all.
So in the head forms load event procedure add this:
Private Sub Form_Load()
Me.DetailSubformControl.SourceObject = "Table1Detail"
Me.DatasheetSubformControl.SourceObject = "Table1Datasheet"
End Sub
Option3: Use Link Master/Child Fields
You could use the properties Link Master Fields and Link Child Fields of the detail subform control.
Therefor you have to create a textbox control named ID on the head form and for cosmetic aspects hide it by setting its property Visible to False.
This new control will be bound to the detail subform control:
Set the property Link Master Fields and Link Child Fields of the detail subform control on the head form both to ID.
The Form_Current procedure of the listview form then only contains this:
Private Sub Form_Current()
' Set the value of the hidden control 'ID' on the head form,
' which is bound to the detail subform control, to the selected ID.
Me.Parent.ID.Value = Me.ID.Value
End Sub
You should handle filtering of the detailview on the Listview_Current event. That event fires as soon as Listview changes records.
You can either set up an event handler for the Listview_Current event on the listview's form module, or use WithEvents in the parent form to listen to that specific event.
If you choose the latter, note that it's required that Listview has a form module, else the events won't fire.
Given that your ID field of the datasource is ID and the detail subform control is named DetailSubformControl, this example works.
Place this code to the Form_Current event of the listview subform (which is fired on every record you move to):
Private Sub Form_Current()
' Set a reference to the detail subform control
With Me.Parent.DetailSubformControl
' Set the filter of its contained form to the current ID of the listview.
' The "'" are only necessary if it is a text and not a numeric field.
.Form.Filter = "[ID] = '" & Me.ID.Value & "'"
.Form.FilterOn = True
End With
End Sub
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?
I have a continuous form in Access. The detail is set up with a button in the detail for every record. The button has on-click code to launch a pop-up form with detail about that record. I have record selectors showing, and those show that when I click a command button, the record selector for the current record is selected. I put a stop in my code when the form is opened and the criteria shows the current selected criteria correctly. when the form opens, it opens to the first record in the record set from the main form, instead of the current selected record on the main form.
I have been searching all over, figuring this has to have been answered, but I can't seem to find a solution that will work right for me.
Below is my code. If anyone can assist, I would really appreciate it.
Private Sub cmdOpenDetails_Click()
Dim rst As Recordset
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "frmAssetDetail"
With rst
Me.txtAssetTag.SetFocus
stLinkCriteria = Me.txtAssetTag.Value
DoCmd.OpenForm stDocName, acNormal, "Forms![frmAssetDetail].AssetTag = ' " & stLinkCriteria & " ' "
Forms!frmAssets.Visible = False
End With
End Sub
One way to solve this:
The command button opens a form with the ID parameter, as in:
DoCmd.OpenForm "frmAddresseeEdit", , , , , , Me.ID
The form (in this case "frmAddresseeEdit") has as its Record source qrySelectedAddressee
This query has the fields required by the form and its criterion is
id = [Forms]![frmAddresseeEdit].[OpenArgs]
I'm working on a legacy database. Specifically, changing a report. I have identified the queries/tables the report is based on. One of the tables has all the hallmarks of being a temprary table generated by a macro of which there are many dozens.
I've been able to identify the Append Query which generates the table and the macro that runs the query. Now I would like to find which form event fires that macro. Does it run everytime the report is generated or once a week or once a quarter or...
There is nothing in the macro behind the "button" that prints the report and no events are fired in the report.
I can iterate over every control in every form, but what property am I looking for?
Any pointers/key word guidance would be appreciated, thanks.
Some notes, it will be easier to find the relevant button when you have the name of the macro:
Sub FindMacros()
For Each f In CurrentProject.AllForms
DoCmd.OpenForm f.Name, acDesign
Set frm = Forms(f.Name)
For Each ctl In frm.Controls
If ctl.ControlType = acCommandButton Then
Debug.Print ctl.OnClick
End If
Next
DoCmd.Close acForm, f.Name, acSaveNo
Next
End Sub