MS Access VBA - Open form and get value from computed textbox

54 views Asked by At

This is about a more complex project but I managed to narrow down the problem to the following case.

In an Access form (fSearch) there is a button (bCheck). The bCheck_Click event code aims at opening an existing excel file and fill in there a specific value.
I can get this value from a computed textbox (tSumAmount) of another form (fMain).

So at 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 coping the value, tSumAmount still has not been computed.
If code is interrupted, then it is computed and if the code continues (F5) then the value is copied correctly.

It seems like tSumAmount is not computed until bCheck_Click ends. Which 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.

So the question about the computed textboxes is, when are they actually computed? I am missing this time point and I don't know even if what I need is possible. Any explanation appreciated. Also if this is not possible, 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
1

There are 1 answers

0
mits On BEST ANSWER

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