Query using field from current open form using VBA Function

Frenstall

New member
Local time
Today, 01:44
Joined
Oct 7, 2005
Messages
9
I am trying to resolve an issue of being able to use a query from many different forms where the query is dependent on the date selected in the current open form. Right now it calls the function getDate() which works fine. Unfortunatly the function is called before the form is fully loaded and the control I want to pass has no value/doesn't exist yet and I get a
"Runtime error 13 Type-Mismatch"

Here is the function code:

Code:
Function getDate() As String
  
   If fIsLoaded("F_SupplierData") Then
        getDate = Form_F_SupplierData.txtDate
    Else
        getDate = "01/01/1901"
    End If

End Function

Function fIsLoaded(ByVal strFormName As String) As Integer
'Returns a 0 if form is not open or a -1 if Open
    If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> 0 Then
        If Forms(strFormName).CurrentView <> 0 Then
            fIsLoaded = True
        End If
    End If
End Function

Is there a way to detect the state of the text field itself (if it is open, closed, dirty)? Or if anyone has a better way to tie a query to multiple forms?
 
Thank-you for the help Gizmo!

I also realized that the field I was trying to access (txtDate) is calculated from a combobox selected by the user. Cosequently when the form loaded the txtDate field contained #error since the calculation contained a Null value. I modified my code to detect if the combobox the user selects is null and it now runs ok. Unfortunatly this is a slow way to perform what I want to do so again if anyone knows a more efficient way that would be greatly appreciated.
 
Why does your function return the date as a string?
 
I really doesn't matter, it could be a date and it would still work fine. The query that captures the funtion return performes a datediff so a string is acceptable. I am not sure if there is a difference time wise.
 

Users who are viewing this thread

Back
Top Bottom