Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Help   Contact   Merch   Join   Order   Logon   Forums   
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Recordsets
Anne Cowden 
    
32 days ago
Is it possible to reference a column in a combobox within a recordset loop.
A Toykan  @Reply  
      
32 days ago
Yes, it is possible, but in a loop you need to make difference between your recordset and the combobox itself. So here are your options:
1. How to reference combobox columns
In Access, columns are zero-based, so you can referenc them like that:
Me!MyCombo.Column(0) 'First column
Me!MyCombo.Column(1) 'Second column
and so on. And this works regardless if you are inside the loop or not, provided that MyCombo is bound to the recordset.
Now, if you are looping through recordset:

Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
Do While Not rs.EOF
'Move form to current record
Me.Bookmark = rs.Bookmark

'Access ComboBox column
Debug.Print Me!MyCombo.Column(1)

rs.MoveNext
Loop

Inside the loop you set the form's bookmark to the current record and then you can reference combobox columns as explained above. Also beware of these:
Value property of the combobox will always give you the bound column, so if you need other columns, you have to use the syntax above. Also make sure that ColumnCount of the combobox is set correctly so that the column that you want to reference actually exists. But regardless of other settings, the syntax remains the same. And it works whether your combobox is bound to a table/query or to a value list.
Anne Cowden OP  @Reply  
    
32 days ago
Many thanks A for your speedy reply, I have been struggling with this for a few days. Our customers are charged different rates for each trade. But sometimes a roofer, for example will help a groundsman and the customer will be charged for 2 groundsmen. I have set up a trade combo box in the labour form which allows the user to choose a trade.

My material form has a field named description which details the items used. My labour form also has a field named description which I want to display the trade. My record set uploads the materials to the Invoice Detail table perfectly and the labour form uploads the number of hours and the hourly rate but leaves the description field blank. In Richard's Work Order Seminar video he totals the hours but I want to list them separately in the Invoice Detail Table. Is there a better way to do this other than referencing the combo box?


A Toykan  @Reply  
      
32 days ago
I think your problem is slightly different than the original combobox question.Yes, you could access the combobox column if you wanted, for example:
Me.cboTrade.Column(1)
but I wouldn't advise using the combo box to do that, and instead use the combo box's bound field.

The labour record needs to have the TradeID, and when creating your invoice detail records you can use the trade description from the trade table. For example, your insert statement for the invoice detail table would have that fields value come from the lookup table, rather than your form. This also covers your roofer/groundsman example better too, because the trade that was actually done, and the trade you bill for could be completely different.

I would suggest storing both the 'Actual' and 'Billing' trades, and then you can use whichever one you need, rather than relying on whatever form is open at the time.

Let me explain with an example
Assume taht your Labour table has LabourID, InvoiceID, TradeID, Hours, HourlyRate and trade table has TradeID, TradeName, HourlyRate
When transferring to InvoiceDetail on the SQL side you can use
INSERT INTO InvoiceDetail(InvoiceID, Description, Quantity, UnitPrice)
SELECT Labour.InvoiceID, Trade.TradeName, Labour.Hours, Labour.HourlyRate
FROM Labour
INNER JOIN Trade
ON Labour.TradeID = Trade.TradeID;

This way you are not dependent on the Combobox. Because the Combobox is only a user interface tool. It is not a data source.

As a second perspective, you could use TempVars -which Richard really likes- and in the Combobox AfterUpdate event you can set
TempVars!CurrentTradeID = Me.cboTrade.Column(0)
TempVars!CurrentTradeName = Me.cboTrade.Column(1)
TempVars!CurrentTradeRate = Me.cboTrade.Column(2)
Then, when creating the Invoice Detail
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("InvoiceDetail")

Do While Not rs.EOF
rs.Edit
rs!Description = TempVars!CurrentTradeName
rs!UnitPrice = TempVars!CurrentTradeRate
rs.Update
rs.MoveNext
Loop
can be used. But, if you are only passing the selected trade between forms during invoice creation, TempVars can be used. However, for billing data I would store the TradeID in the Labour table and use that as the source. That would be my approach.
Anne Cowden OP  @Reply  
    
32 days ago
Thank you so much, you are an absolute star. I will try that and let you know how I get on. xx
A Toykan  @Reply  
      
32 days ago
Glad to help.
Anne Cowden OP  @Reply  
    
31 days ago
Hi, I took your example for the sql option and made sure the ID fields were in the tables. My record set is still uploading the materials to the InvoiceDetailT but I'm not sure where to put your SQL. To try it out I created a query containing the LabourT and the TradeT. When I ran the query it appended the records to the InvoiceDetailT and the trade descriptions, hours and hourly rates worked fine but it did not bring in the invoiceID. Can the sql statement be put inside the record set? I have commented out the original lines in the recordset, taken from Richard's video but I think that is where the LabourT got the InvoiceID from. What am I missing?
A Toykan  @Reply  
      
31 days ago
For example, let's assume the transfer is performed from the Click event of a button named cmdTransferLabour.
Private Sub cmdTransferLabour_Click()
Dim strSQL As String
Dim lngInvoiceID As Long
'Get the current invoice ID from the form
lngInvoiceID = Me.InvoiceID
strSQL = "INSERT INTO InvoiceDetailT (InvoiceID, Description, Quantity, UnitPrice) " & _
"SELECT " & lngInvoiceID & ", TradeT.TradeName, LabourT.Hours, LabourT.HourlyRate " & _
"FROM LabourT INNER JOIN TradeT ON LabourT.TradeID = TradeT.TradeID " & _
"WHERE LabourT.InvoiceID = " & lngInvoiceID
CurrentDb.Execute strSQL, dbFailOnError
'Mark transferred records
CurrentDb.Execute "UPDATE LabourT SET Transferred = True WHERE InvoiceID = " & lngInvoiceID
MsgBox "Labour records transferred successfully.", vbInformation
End Sub
In the SELECT statement, the InvoiceID is taken directly from the current form and embedded into the SQL statement instead of being read from the LabourT table. This ensures that every record inserted into InvoiceDetailT is associated with the correct invoice.
Compared with the traditional recordset approach (rs.AddNew / rs.Update inside a loop), executing a single SQL INSERT INTO ... SELECT statement is much more efficient when transferring multiple records. The database engine performs the entire operation in one step, resulting in faster execution and less VBA code.
The dbFailOnError option is also recommended. If an error occurs during the insert operation for example, because of a data type mismatch or a validation error, Access raises an error instead of silently ignoring the failed records. It gives you debugging oppotunity and makes it much easier.
Finally, consider adding a Yes/No field named Transferred to the LabourT table. After the transfer, you can mark the processed records with:
CurrentDb.Execute "UPDATE LabourT SET Transferred = True WHERE InvoiceID = " & lngInvoiceID
This prevents the same labour records from being transferred multiple times if the button is clicked more then once, and also provides a simple way to identify which records have already been processed.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
 
What's This?

 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 9/6/2026 9:16:19 AM. PLT: 1s