I have a basequery named qryFghEkonomi and a global variable, dim dak as long and a module shown below.
I then use this code in a form, it all works fine until the moment when I want to save the value to the variable and then display it in a textbox. According to the msgbox the value of dak i 0
and when trying toi assign the textbox a value i get the message can not assign that objekt a value. The funny thing is if I pull up the question manually and run it with the form open after clicking the button ( so the qry is changed according to GetDAK() ) i get the correct value but it will not be saved into the variable.
Option Compare Database
Option Explicit
Public Function GetDAK() As Integer
Dim qry As QueryDef
Dim DB As Database
Dim sSql As String
Set DB = CurrentDb()
Set qry = DB.QueryDefs("qryFghEkonomi")
qry.SQL = "SELECT intDAK FROM [tblFghEkonomi] " & _
"WHERE [intFastighetsnummer]=[forms].[frmNyAlgh].[intFastighetsnummer];"
dak = DMax("[intDak]", "qryFghEkonomi")
Set qry = Nothing
End Function
In the form:
Private Sub cmdGetDAK_Click()
On Error GoTo Err_cmdGetDAK_Click
Call GetDAK
Me.txtDAK = dak
MsgBox dak
Exit_cmdGetDAK_Click:
Exit Sub
Err_cmdGetDAK_Click:
MsgBox Err.Description
Resume Exit_cmdGetDAK_Click
End Sub