troubleshooting Question

MS Access VBA Dynamically Specifying The Column Name to Write To

Avatar of Gary Croxford
Gary CroxfordFlag for United Kingdom of Great Britain and Northern Ireland asked on
VBAMicrosoft Access
3 Comments1 Solution49 ViewsLast Modified:
Thank you for looking at my question,

I have put together a small split database to which the user imports data from a number of text files.
One table, a monthly analysis of inventory movements, is created dynamically based upon user specified criteria which means the number of columns may vary depending upon the number of months in the sample (one column per month.)

I am struggling to get the table to populate dynamically. the code I am using is:

    
    rs_PeriodIssue.AddNew
    rs_PeriodIssue("Item").Value = strItemOld
    rs_PeriodIssue("Item Desc").Value = strItemDesc
    rs_PeriodIssue("WH").Value = strWarehouse
    
    intArrayElement = 0
    For i = 0 To intPeriodRange
        strPeriod = CStr(intColumns(intArrayElement, 0) & "_" & intColumns(intArrayElement, 1))
        
        rs_PeriodIssue(strPeriod).Value = CLng(strValues(i, 1))
        intArrayElement = intArrayElement + 1
    Next
    
    rs_PeriodIssue("Summed Issue").Value = CLng(Format(intSummedIssue, "0.0000"))
    rs_PeriodIssue("Average Issue").Value = CLng(Format(intAvgIssue, "0.0000"))
    rs_PeriodIssue("Sample Period").Value = CLng(intPeriodRange + 1)
    rs_PeriodIssue("Days in Sample Period").Value = CLng(intDaysInPeriod)
    rs_PeriodIssue("Year From").Value = CLng(intYearFrom)
    rs_PeriodIssue("Period From").Value = CLng(intPeriodFrom)
    rs_PeriodIssue("Year To").Value = CLng(intYearTo)
    rs_PeriodIssue("Period To").Value = CLng(intPeriodTo)
    rs_PeriodIssue.Update

The thing falls over at this point:
                    For i = 0 To intPeriodRange
                        strPeriod = cstr(intColumns(intArrayElement, 0) & "_" & intColumns(intArrayElement, 1))
                       rs_PeriodIssue(strPeriod).Value = CLng(strValues(i, 1))
                        intArrayElement = intArrayElement + 1
                    Next

with an error Type 13 Mismatch

In vba how do I (can I) dynamically specify the column name to which I wish to write an item of data?
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros