Private Sub btnImportShortForm_Click() 'This sub imports data from the FFWB Short For (1 Viewer)

GaleT

Registered User.
Local time
Today, 08:42
Joined
Oct 18, 2019
Messages
72
I have written the following to extract data from specific cells in an Excel Workbook into an Access form. It works until I try to write the extracted values to the form fields. I get a Compile Error "Methos or Data Member Not Found" on "Me.FormFieldName.Value = CellVal". How can I correct this?

Code:
"Private Sub btnImportShortForm_Click()
'This sub imports data from the FFWB Short Form

Dim FilePath As String, SheetName As String, CellName As String, FormFieldName As String
Dim CellVal As Variant
Dim xl As Object
Dim wb As Object
Dim ws As Object
Dim FromCell As Variant, ToFormField As Variant
Dim n As Integer

FromCell = Array("D6", "D7", "D8", "D11", "D19", "D23", "D25", "D26", "D33", "D44", "D46")

ToFormField = Array("Txt_Group", "Txt_Customer", "Txt_ProjNum", "Txt_OppNum", "Txt_Product", "Txt_EngPlannerName", _
"Txt_SalesName", "Txt_BusUnit", "Txt_Amount", "Txt_InterIntra", "Txt_SWC")

'Path to Short Form & Sheet name. Sheet name never changes but the path will be selscted each time.
FilePath = "S:\2019\AccessTestFolder\OR1940044 FFWB SF.xlsm"
SheetName = "FFWB SF"

Set xl = CreateObject("Excel.Application")
Set wb = xl.Workbooks.Open(FilePath)
Set ws = wb.Sheets(SheetName)

    For n = 0 To 10
        
    'Get Value from Workbook
    CellName = FromCell(n)
    CellVal = ws.Range(CellName)
    
    'MsgBox "Cell " & CellName
    'MsgBox "Value " & CellVal
    
    'Get Form Field Name from array
    FormFieldName = ToFormField(n)
    
    'Write value to Form Field
    Me.FormFieldName.Value = CellVal  <--- Error here

    Next n

wb.Close
xl.Quit
Set ws = Nothing
Set wb = Nothing
Set xl = Nothing

End Sub
Gale
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 08:42
Joined
Aug 30, 2003
Messages
36,124
Try

Me(FormFieldName).Value = CellVal
 

GaleT

Registered User.
Local time
Today, 08:42
Joined
Oct 18, 2019
Messages
72
Thank you pbaldy, that solved the problem :)
Can you explain why this works? If you just know it does that's fine... I am just curious because I would never have thought to try this.
Gale
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 08:42
Joined
Aug 30, 2003
Messages
36,124
The way you had it, it expected the actual name of the control, not a variable. The dot following "Me" tells it what follows is a "member or method" of the object, thus the error message when it wasn't. The syntax I posted allows the variable to be resolved to the desired name.
 

GaleT

Registered User.
Local time
Today, 08:42
Joined
Oct 18, 2019
Messages
72
Thank you, that makes perfect sense. :)


Gale
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 08:42
Joined
Aug 30, 2003
Messages
36,124
Happy to help Gale!
 

Users who are viewing this thread

Top Bottom