In an Access form (fSearch
) there is a button (bCheck
). The bCheck_Click
event code aims at opening an Excel file to fill a specific value.
I can get this value from a computed textbox (tSumAmount
) of another form (fMain
).
First I open fMain
, then I open the Excel file, and then I try to copy tSumAmount
's value in the Excel file, which never happens.
Using Debug.Print
I can see that right before copying the value, tSumAmount
has not been computed.
If code is interrupted, then it is computed and if the code continues (F5) the value is copied.
It seems tSumAmount
is not computed until bCheck_Click
ends. This is confusing as the fMain
's Form_Load
event is fired first and there is plenty of time for it to finish before bCheck_Click
attempts to copy the value.
When are the computed textboxes computed?
I need this to work somehow.
Sub bCheck_Click()
Dim oXl As Excel.Application
Dim oWb As Excel.Workbook
Search_It
Set oXl = New Excel.Application
oXl.DisplayAlerts = False
oXl.Visible = True
Set oWb = oXl.Workbooks.Open("full_path_of_excel_file")
'-> At this point tSumAmount has not been computed yet. Tried Sleep, still no luck.
Debug.Print "(" & Forms("fMain").tSumAmount & ")" '-> output: ()
With oWb.Worksheets(1)
.Range("A1") = Forms("fMain").tSumAmount
End With
End Sub
Sub Search_It()
'do stuff
DoCmd.OpenForm "fMain"
End Sub
Based on @FunThomas's tip the working code is the following:
Sub bCheck_Click()
Dim oXl As Excel.Application
Dim oWb As Excel.Workbook
Search_It
Set oXl = New Excel.Application
oXl.DisplayAlerts = False
oXl.Visible = True
Set oWb = oXl.Workbooks.Open("full_path_of_excel_file")
Forms("fMain").Recalc
With oWb.Worksheets(1)
.Range("A1") = Forms("fMain").tSumAmount
End With
End Sub
Sub Search_It()
'do stuff
DoCmd.OpenForm "fMain"
End Sub