I have a form called Main. On it I have
a text box to hold the record ID
a combobox to select from existing records or enter a name for the new record
a text box to hold the name from the combobox
a button to open a form that lets us enter additional data and create a new record
a button to open a form that lets us edit an existing record
I cascade the row source for the list in the combobox. If the entry is not in the list,
the NotInList event
Copies the name in the combobox is moved to a text box on Main
Loads the create form's and copies the name from Main to the appropriate field on the create form.
All that works fine. The problem is that when I hit Save on the create form, I cannot write values back to the Main form correctly. I want to write the name that came from the combobox back to the combobox on Main, and write the record ID to the text field on Main.
It's two lines of code. If I change their order, the output changes. Either the combobox ends up blank, or the record ID text field ends up blank.
While I was debugging this I can see the values exist, but I could not make the assignments. The last time I tried combobox.text it complained about the value not being
in the list, so I ran the query for the row source, but the record did not show up in the combobox list, but is in the table. See the two lines of code (bold) below.
Private Sub cmbSaveClose_Click()
Dim x As Integer
x = MsgBox("Are you sure you want to save changes?", 4, "Exit?")
'VbNo constant throws back #7
'VbYes constant throws back #6
Me.Txt32 = DLookup("InsuranceCarrierContractID", "ICCDupRecordCheckQ")
If x = 7 Then
Exit Sub
End If
If IsNull(Me.Txt32) Then
Me.Txt31 = Form_frmMain.Txt65
DoCmd.RunCommand acCmdSaveRecord
**Forms!frmMain.cboInsuranceCarrierContract = Me.InsuranceCarrierContractID
Forms!frmMain.Txt66 = Me.InsuranceCarrierContractID**
DoCmd.Close
Forms!frmMain!InsuranceCarrierContract.RowSource =
" SELECT InsuranceCarrierContract.ContractNumber " & _
" FROM InsuranceCarrierContract " & _
" ORDER BY InsuranceCarrierContract.ContractNumber;"
'Other stuff,works fine.
Form_frmMain.cboInsuranceCarrierContract.Locked = False
Form_frmMain.cboInsuranceCarrierContract.BackColor =
Form_frmMain.cboInsuranceCarrier.BackColor
Else
Me.Undo
DoCmd.Close
End If
End Sub
This worked for David, so adding it as an Answer:
ReQuery and ReFresh the form after you change the data.
Me.Requery
Me.Refresh
from within the form module, or externally:
Forms("MyFormName").Requery
Forms("MyFormName").Refresh
http://msdn.microsoft.com/en-us/library/office/ff191903.aspx
http://msdn.microsoft.com/en-us/library/office/ff836021.aspx
Related
I have two Comboboxes on a from: txtKategorie and txtTyp.
The values for the frist Combobox (txtKategorie) are fix!
I want the values of the second Combobox (txtTyp) to change according to what the user chooses in the first one.
If the user chooses "Datalogger" the second combobox should only contain "Base Layer Classic" and "Base Layer Plus" as can be seen in the image.
The same idea is true for "Accelerometer".
I've put my code in the AfterUpdate Event of the first Combobox:
If txtKategorie.Value = "Datalogger" And txtTyp.ListCount = 0 Then
i = 1
Do While txtTyp.ListCount < DCount("ID", "tblNomenklatur", "[Kat] = 'K'")
txtTyp.AddItem DLookup("[Typ]", "tblNomenklatur", "[ID] =" & i And "[Kat] = 'K'")
'And "[Kat] = 'K'"
i = i + 1
Loop
When the form opens only the first Combobox "txtKategorie" has Values. When the user chooses Datalogger the code checks how many records in the table have the [Kat] = "K" to define how long the Do While-Statement will run. Then the "txtTyp.AddItem"-Statement should add "Base Layer Classic" and "Base Layer Plus" to the "txtTyp" Combobox. But unfortunately the Code doenst work. There is a problem with the Dlookup-Statement containing tow criterias. If i remove either one of the two criterias the Code works but delivers wrong results for the second Combobox obviously. If i leave it like this the second Combobox stays empty.
Does someone know what im doing wrong?
You can do it easily by below code. Change table name with your table name.
Private Sub txtKategorie_AfterUpdate()
Me.txtTyp.RowSource = "SELECT DISTINCT Table1.Typ FROM Table1 WHERE Table1.Kategorie='" & Me.txtKategorie & "'"
Me.txtTyp.Requery
End Sub
Or you can do it graphically from row source query builder. Below are steps.
Combobox txtKategorie is fix. Fine!
For second combobox txtTyp follow the below steps.
Select combobox txtTyp. From property windows select Row Source then click on query builder ... small three dot. See screenshot.
In query builder window build a query from your data table like screenshot and set criteria for Kategorie column is [Forms]![Form1]![txtKategorie] Save and close the query bulder window.
Now for Combobox txtKategorie in After Update event write below line to requery txtTyp. You are done!
Private Sub txtKategorie_AfterUpdate()
Me.txtTyp.Requery
End Sub
I have a form I want to use as a guide to search for a record then pull the record up to the top form as a highlighted line with the rest of the records to follow. Additionally, I want the searchable text box that I'm searching in to automatically jump to the next record without hitting enter or clearing the textbox. I want that part to be automated. So I would be continuously entering in numbers for thousands of records and looking up their information.
Some other info, my form is non-editable but the text box is.
I've tried using a form with some code but can't get it gives me an error saying my private sub is not recognized as a valid field name or expression.
Private Sub Recsch_AfterUpdate()
On Error GoTo Err_ErrorHandler
Dim ItemSelected As String
ItemSelected = Forms![Inventory_Status].Recsch
Recsch = Null
Me.Filter = "[RecId] = '" & ItemSelected & "'"
Me.FilterOn = True
Err_ErrorHandler:
Exit Sub
End Sub
I just want my code to take me to the next line of the form without having to click anywhere. So it would enter in the data on the box, search in my form, highlight the line, bring it to the top, then highlight my box again so I can enter the next number without pressing enter.
I got a Database, where I apply to a name a Main and Subgroup.
When I enter a MainGroup f.e. Granades, just subgroup elements like "attack-granades" etc. should be shown.
In genereal it works by writing in this into the MainGroup-Combobox at my Form.
Private Sub MunHauptgruppeRef_AfterUpdate()
Me.MunUntergruppeRef.Requery
Me.MunUntergruppeRef.RowSource = " SELECT UnterGrpNR, UnterGrpName FROM tbl_UnterGruppen WHERE UnterHauptGruppenNr = " & MunHauptgruppeRef.Value & " ORDER BY UnterGrpName ASC"
The Problem is, if I load the datas in my form again, it just shows the related Number to the "Sub-Combobox data" 1( f.e. 35 for Attack-Grenade ) , but not the Name itself. After I reselect the entry in my Main-Combobox(Grenade), it shows the right sub-data which was saved. 2
Tried Requery on Form_Load or Requery of the Combo-Boxes itself. nothing helped so far.
Made some Video3
You need to add the code from Private Sub MunHauptgruppeRef_AfterUpdate() to your Form_Current event, this will refresh your combo box row source to the current value of your MunHauptgruppeRef combobox as you cycle through your records. Also you need to call the Me.MunUntergruppeRef.Requery after you've set the Me.MunUntergruppeRef.RowSource.
You can also add a check when your in a New Record, for the code not to run on the Current Event. See here https://learn.microsoft.com/en-us/office/vba/api/access.form.newrecord
I just learning how to create forms in Microsoft Access 2013 and using VBA programming to control but I'm having an issue I don't quite understand.
I have created a form with a List Box where the source originates from a query that contains the following fields the Query Builder in order from left to right:
|ParentNumber|ParentName|ChildNumber|ChildName|
|------------|----------|-----------|---------|
|------------|----------|-----------|---------|
|------------|----------|-----------|---------|
Some fields from the Query are hidden with a Column Width of 0".
I also have 4 Text Box below the List Box corresponding to the appropriate ParentNumber, ParentName, ChildNumber, and ChildName values.
When I select a record in the List Box, it populates the data to the appropriate Text Box.
When the form first loads, the first row in the List Box is selected:
Private Sub Form_Load()
On Error Resume Next
DoCmd.GoToRecord , , acFirst
Me.List = Me.List.ItemData(0)
End Sub
The issue is that if I select a different row, then close the form, and reopen the form, the first row in the List Box is overwritten with the last selected row before the form is closed.
Even if I start out with any other row selected initially when the form opens, the first row is always overwritten by the last selected row when the form is closed.
The following subroutine handles the update of the Text Box data:
Private Sub List_AfterUpdate()
Dim rowIndex As Integer
rowIndex = Me.List.ListIndex
Me.textBox_ParentNumber = Me.List.Column(3, rowIndex)
Me.textBox_ParentName = Me.List.Column(4, rowIndex)
Me.textBox_ChildNumber = Me.List.Column(6, rowIndex)
Me.textBox_ChildName = Me.List.Column(7, rowIndex)
End Sub
I found a somewhat similar problem to mine, but I tried the suggested solution, which didn't seem to work: MS Access - First record in table is overwritten on form close
I'm completely baffled as to what could cause this based on the code above.
What's my issue?
Thanks.
Few things
Private Sub Form_Load()
'remove this so you can see errors.
'On Error Resume Next
' this goes to the first record of the *form*, not the list.
' you might want this, or not.
DoCmd.GoToRecord , , acFirst
' ItemData returns the data in the *bound column* of the list,
' the data from one specific column. the list is set to that
' data every time the form loads. not what you want; remove it.
'Me.List = Me.List.ItemData(0)
End Sub
This should get rid of the problem. What you want to do next is another question.
Your form should be bound to a record set (table or query).
I had same problem, After hours of verification -
In my case - the problem was that the MainForm , And SubForm (table) - was binded to the same recordset. I just Un-Binded data RS from MainForm.
I have a list box and its row source is "tblitems" with bellow fields.
Now I like when right click on this list box and select one option for example "new task" it opens new task form and automatically Grab the item number from a list box and fills related text box "item number" in "new task" form that is bounded to table "tbltask"
Now when I press "Apply" button it insert new record in "tbltask".
tblItems
item number (pk)
item name (text)
tbltask
task number (auto number,pk)
item number
enter image description here
Getting a value from a list box in a form is:
Me.List_Box_Name.Value
Or if you have unbound values then get it based on the column:
Me.List_Box_Name.Column(2)
or whichever column you need.
You can then populate fields with DLookup or a recordset. Then if you want this to happen when you open a new form, you may want to look into this:
Private Sub Form_Load()
'Stuff you want to happen when that form loads
End Sub
Updated
The following will print out the value each time you click it. You can use this method to trigger a new form to open, or you can have a user click a submit button afterwards.
Private Sub Test_List_Click()
Debug.Print Me.Test_List.Value
DoCmd.OpenForm "Form_Name"
End Sub
Now I'm not exactly sure the best way to add your variable to the opened form. If it were me with my limited knowledge, I would add a global or public string and have the Form_Load() check to see if that string length is greater than 0. If yes then it will populate the field.
Hope that helps
Update 2
Actually this link will help you populate a field from previous form:
MS Access - open a form taking a field value from a previous form