UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Using vba to get single value from a table with criteria and the    
 
   
oso
post Feb 3 2006, 07:26 AM
Post #1

UtterAccess Enthusiast
Posts: 88



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
Go to the top of the page
 
+
ScottGem
post Feb 3 2006, 07:41 AM
Post #2

UtterAccess VIP / UA Clown
Posts: 25,090
From: LI, NY



I don't follow what you are doing here. You define the SQL for a query but don't appear to be using it anywhere. Also, if you are using a value in a textbox as criteria, you need to concatenate it in:

"WHERE [intFastighetsnummer]=" & [forms].[frmNyAlgh].[intFastighetsnummer] & ";"

If qryFghEkonomi is a named query as it appears, then you shouldn't need GetDAK at all. Just use:

Me.txtDAK = DMax("[intDak]", "qryFghEkonomi")

Also a function returns a value so you don't use a CALL with a function. You just assign it. So if you really needed a GetDAK function you would just use:

Me.txtDAK = GetDAK()
Go to the top of the page
 
+
oso
post Feb 3 2006, 07:57 AM
Post #3

UtterAccess Enthusiast
Posts: 88



I define the query and then that is the query that is updated with the sql in qry.sql = ....
When i run the querry manually i get the correct value but i cant get that value saved in a text box or variable

Me.txtDAK = DMax("[intDak]", "qryFghEkonomi") doesn't work allready tried it.

I wanna use the function because i will use the same procedure in many different forms.

the really strange thing is that when i display this in a msg box: MsgBox DMax("[intDak]", "qryFghEkonomi")

the correct value is shown, but i cant put it into a textbox.

Edited by: oso on Fri Feb 3 8:01:17 EST 2006.
Go to the top of the page
 
+
ScottGem
post Feb 3 2006, 08:42 AM
Post #4

UtterAccess VIP / UA Clown
Posts: 25,090
From: LI, NY



Ahh, I see what you mean about updating the query by setting the SQL property of the QueryDe, I missed that. But again, that doesn't work because you need to concatenate the value from the form.

But frankly I don't understand why just assigning the DMax to the control doesn't work. What happens when you try it?
Go to the top of the page
 
+
schroep
post Feb 3 2006, 10:48 AM
Post #5

UtterAccess VIP
Posts: 5,200
From: Denver, Colorado [USA]



I don't see you defining the variable "dak" anywhere. You said it is defined as a global variable; where?

Global variables are defined in code modules (not form modules) and should be defined as:

Public dak As Long

Depending on how/where you are defining it, it may not have scope in your form's module, and may thus not be keeping its value between your two different procedures. Since this is a function, let's use the return value, rather than depending on a global. There may also be some issues with the way you are modifying the query; the change may not be saved when you run your DMAX function.

Try this code:
CODE
Public Function GetDAK() As Integer
  Dim qry As QueryDef
  Dim DB As Database

  Set DB = CurrentDb()
  Set qry = DB.QueryDefs("qryFghEkonomi")
  qry.SQL = "SELECT intDAK FROM [tblFghEkonomi] WHERE [intFastighetsnummer]=[forms]![frmNyAlgh]![intFastighetsnummer]"
  qry.Close
  Set qry = Nothing
  Set DB = Nothing
  DoEvents
  GetDAK = DMax("[intDak]", "qryFghEkonomi")
End Function


Private Sub cmdGetDAK_Click()
  Me.txtDAK = GetDAK()
End Sub
Go to the top of the page
 
+
schroep
post Feb 3 2006, 11:46 AM
Post #6

UtterAccess VIP
Posts: 5,200
From: Denver, Colorado [USA]



Just a note; here is another way to write your GetDAK function, that does not do any query modifications:
CODE
Public Function GetDAK() As Integer
  Dim rst As DAO.Recordset

  Set rst = CurrentDb.OpenRecordset("SELECT Max(intDAK) FROM [tblFghEkonomi] WHERE [intFastighetsnummer]=[Forms]![frmNyAlgh]![intFastighetsnummer]",dbOpenForwardOnly)
  If Not rst.EOF Then GetDAK = rst(0)
  rst.Close
  Set rst = Nothing
End Function
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 23rd May 2013 - 03:23 PM

Tag cloud: