Populating data in worksheet from VAB data entry form - vba

With almost 0 knowledge in VBA I have created a Data Form to enter daily voucher with:
MS Date and Time Picker
TextBox1 for Amount (validated for numeric value only - working fine)
OptionButton1 for Cash
OptionButton2 for Cheque
TextBox2 for the name of person Paid To (maybe I can change it into a ComboBox?)
TextBox3 for Paid For (any value can be entered)
ComboBox1 for Sub Category (list is in Worksheet2 (List) - working fine)
TextBox4 for Remarks (any value can be entered)
CommandButton1 to add the data into Worksheet3(database)
CommandButton2 to show the 'database' worksheet
CommandButton3 to close the form (working fine)
A CommandButton is also in Sheet1(Form) to open the form (working fine).
I want to enter the data into the form and populate this data to Worksheet3(database) as
Date in (B3), Paid To (C4), Paid For (D4), Sub Category (E4), Remarks (G4), Amount (H4).
(F4) is free for a VLOOKUP.
I also want to have a data edit option in the form that may be with an auto-generate voucher number with recall option.
Can any one help me with the appropriate code to do this?
Note: I am creating with Office 2007 but also want to use it in XP Professional

On the first part of your question, it depends whether you want to populate worksheet3 on the click of a button or dynamically as the data is entered into the form. I'm assuming the former as most forms have OK buttons on them. If this is the case then you can write a subroutine based on the button click, to give a simple example:
Private Sub Enter_Details_OK_Click()
Sheets("worksheet3").Range("B3").Value = Calendar1.Value
'Change Calendar1 to the value of your date/time object and change "worksheet3" to the actual name of your worksheet.
Sheets("worksheet3").Range("C4").Value = TextBox2.Value
'...and so on...
You are basically transferring the values from each text box into various cells. I'm not sure what you mean on the second part of your question, that needs further clarification.
Hope this makes sense!

Related

Getting a type mismatch but combo box still searches and displays data. ID column is being used to pull through Name but doesn't work

Error popup after click ok on type mismatch
I am using a combo box to search for records and it is telling me I have a type mismatch. I have been unable to find where this mismatch would be. The combo box still performs the desired function of selecting all records based on their "region"
I am using an ID field which is selected using the combo box but I want to pull through a name as well so I have input an unbound textbox and used VBA on change in the ID to update the name to the corresponding ID in the unbound textbox
Form
For privacy reasons I snipped around some of the other fields showing data.
The VBA in the on_change event in ID goes
Private Sub txtSupplierID_Change()
Me.txtSupplerName.Value = Table("tblSuppliers").SupplierName
Where Table("tblSuppliers").ID = txtSupplierID
End Sub
I'm aware this code could be very wrong but I could not find anywhere else that showed me how to do what I was trying to do. Any suggestions on how to actually do it would be appreciated. If I have not been as detailed as needed or yo have any questions please ask.
Edit for Clarification:
The form is not being used to save data. It is only being used to display data and issue an output to a report.
Code is not just wrong, it is nonsense. Cannot reference a table object like that. And that Where usage is not valid. One way to pull value from a table is with DLookup domain aggregate function. Advise use AfterUpdate instead of Change event.
Private Sub txtSupplierID_AfterUpdate()
Me.txtSupplerName.Value = DLookup("SupplierName", "tblSuppliers", "ID = " & txtSupplierID)
End Sub
Might want to correct textbox name from txtSupplerName to txtSupplierName.
However, should not save both supplier ID and supplier name into this table. There really should be no need for this VBA. Just put DLookup() expression in a textbox ControlSource. For other methods of pulling the supplier name to form, review answer in MS Access Identical Comboboxes for Autofill

AfterUpdate On ComboBox: Display selection without requiring other action

I have a form with a combo box that has approval codes in it.
I created an AfterUpdate event so that when the user selects the approval code today's date will be placed in another field.
The problem is when I tab out of the combo box the date doesn't display in the field unless I click in that field or save the record.
I know I can do a Me.Refresh after the code, but I don't want to save the record until the user is done with everything they need to input.
Private Sub AppArch_AfterUpdate()
Me.DATE_RCVD_ARCH = Date
End Sub
Must reference textbox name so if field and textbox have same name, then your existing code should work and new value display immediately. Apparently that is not the case. I always name controls different from field, like tbxDateRA. Then code can be:
Me.tbxDateRA = Date
or
Me!tbxDateRA = Date
but use the first to trigger intellisense tips.

Setting ComboBox RowSource property to query in "GotFocus()" method leaves blank values in ComboBox Access VBA

I want to populate my ComboBox in an Access form using VBA based on data from another table. Previously to do this I did the following:
In the Property Sheet -> Data tab I filled out Row Source and Row Source Type fields with this information:
Now whenever I clicked on the dropdown for my combobox, it would populate the dropdown list with all of the names from t_people table.
This limited me however to when data changed in the t_people's name column. In order to get an updated list, I must close the form and re-open it so that the query runs again. I have limited the access to this Access file so that the user is only presented with x number of forms, and cannot close/open them or others.
My solution is to remove the query on the form load, and instead run the query every time the combobox gains focus, has a click event or something of the same sorts. I did this with the following event in VBA:
'Run when the "name" combobox gains focus
Private Sub nameCb_GotFocus()
[nameCb].RowSource = "SELECT name FROM t_people"
End Sub
I have set breakpoints and this code does run. However, the the combobox is not populated after it does. How can I get the combobox to populate with the values from a query for each time the combobox gains focus?
Set the RowSource in design and add a .Requery when entering the control.
Private Sub nameCb_Enter()
nameCb.Requery
End Sub

Link cell value to form combobox value

Might be easy, but how do I link a form comboxbox value to a cell or a dropdown list of cells ? In my case a 1 or 2 dimensional with month - year or month txt - month nr value or array needs to be linked to a form combobox.
The result should be that when I change the form combobox value it also updates the excel cell or cells (the 2 cells combination above) and also the other way around, when I update the cell or cells, it also updates the form combobox value.
Atm, I only have it one way, from form combobox to value by using the built in sub.
Sub ComboBox1_Change()
ThisWorkbook.Sheets("List").Range("G2").value = Form1.ComboBox1.Value
End Sub
After some digging around, I have found the answer to be in the Combobox properties window, ControlSource. I put there in my case 'List'!G2 and combined with the Sub ComboBox1_Change() it updates the month both ways.

Combobox_Change function for combobox's created with .Controls.add function*

Let me start by saying that I have searched far and wide for hours before answering this question.
Situation (all working) -
User is to specify the number of apples in a basket
Example - user specifies 2 apples. This creates two pages within a multipage with comboboxes and textbox's.
The combobox's are populated with a database of apple names. The user chooses one.
Desired:
When a user chooses an apple from the combobox I would like the textbox's on that multipage to populate with the apples characteristics. In other words I need my code to fire any time a user selects an apple from this combobox. This is not a combobox created from the toolbar, so I cant seem to use "private sub Combobox_Change".
Below is the code that I use to create the combobox:
Set ComboBoxL = MultiPage1.Pages(pgCount).Controls.Add("Forms.combobox.1", _
"ComboBoxL" & pgCount)
With ComboBoxL
.Top = 1 * FromTop
.Left = LeftDist
.List = Sheets("TestLightList").Range("A1:A100").Value
This is the code that I use to pull the value into the textbox, it works in testing (just running it in another part of the code) but doesnt work when needed because I dont know how to tell VBA to fire it off when the user selects an item from the combobox above
userform2!textboxC1.Value = Application.index(Range("'TestLightList'!$A$2:$E$500"), _
Application.Match(userform2!ComboBoxL1.Value, Range( _
"'TestLightList'!$A$2:$A$500"), 0), 2)
Note that in this snippit of code im working off of the first combobox. The formula creates a series of them in numerical order.
Thank you!