p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Pro VB Databases (http://p2p.wrox.com/forumdisplay.php?f=81)
-   -   pass the result of a query to a variable ... (http://p2p.wrox.com/showthread.php?t=29861)

eusebio May 28th, 2005 02:54 PM

pass the result of a query to a variable ...
 
I'm trying to pass the result of a query to a variable.
My code is something like that:

Public Function get_last_id(what) As Double
         Dim ds As Database
         Dim myX As Double
    Set ds = OpenDatabase("c:\itc\bd\gestiuneIOMC.mdb")
Select Case what
    Case Is = "ref"
    myX = ds.Execute("select max(nrreferat) from tabelreferate;")
    MsgBox myX
    ds.Close
End Select
End Function

when I try to execute the function, I receive de message "Expected function or variable"
What should I do ?
Thanks in advance ...
Eusebio.

mike_abc May 30th, 2005 12:41 AM

Hi Eusebio,

Check the Select Case syntax - i think yours is wrong.

Select Case <variable_name>
  Case <value>
    ....




BrianWren May 31st, 2005 11:54 AM

Code:

Public Function get_last_id(what) As Double  ' What type of thing is “what”?

    Dim ds  As Database
    Dim myX As Double

    Set ds = OpenDatabase("c:\itc\bd\gestiuneIOMC.mdb")

    Select Case what
        Case Is = "ref"
            myX = ds.Execute("SELECT MAX(nrreferat) FROM tabelreferate;")
            ' don’t use a semi-colon in the SQL, above.
            MsgBox myX
            ds.Close
    End Select

End Function

Do you know which line returns the error?
If what is a string, the Case should be
Code:

        Case "ref"
Your .Execute statement returns a recordset (I’m pretty sure). A double can't hold a recordset.

If you want to do something really similar in syntax, create a function which returns a Double, say “MaxReferat(),” and within that function, return a recordset into a local variable, read the value of the one record, and return that as the function's return value. Then you would have:
Code:

        Case Is = "ref"
            myX = MaxReferat()
            MsgBox myX


eusebio June 1st, 2005 02:10 PM

Thanks Brian,

I realised myself it was a mistake to pass a recordset to a double. The case syntax works perfectly as it is ... I changed it like you said ... it works the same ...
Now I have something like that
Public Function get_last_id(what) As Double
Dim ds As Database
Dim dds As Recordset
Dim myY
Set ds = OpenDatabase("c:\itc\bd\gestiuneIOMC.mdb")

    Select Case what
        Case "ref"
            k = "select max(nrreferat) as [kk] from tabelreferate;"
            Set dds = ds.OpenRecordset(k)
            myY = dds!kk
            MsgBox myY
            ds.Close
        Case "data"
            k = "select min(datareferat) & max(datareferat) as [kk] from tabelreferate;"
            Set dds = ds.OpenRecordset(k)
            myY = dds!kk
            MsgBox myY
    End Select
End Function

Now it works ... if you have any sugestions please feel welcomed ... criticise also ...
I have to do an app and I'll use pretty much SQL (doesn't scare me cause I'm familiar with oracle) and I have a problem resolving what access can't do ...
DO you have any sugestions how to easy work with queries in VB6 & Access database ?

Thanks again,
Eusebio.


BrianWren June 1st, 2005 05:16 PM

Last question 1st: YES!
Use the query builder, then, having created a query, switch to SQL view to see how Access does the task at hand.
You can create queries using query wizards, then open them for design and switch to SQL view to see how that task was done.
This is the best way I know to ascertain Access' way of doing things.




Next, this:
Code:

            k = "select max(nrreferat) as [kk] from tabelreferate;"
            Set dds = ds.OpenRecordset(k)
            myY = dds!kk

            could be
Code:

            k = "SELECT MAX(nrreferat) FROM tabelreferate;"
Code:

            Set dds = ds.OpenRecordset(k)
            myY = dds.Fields(0)
            ' Or (I believe):
            myY = dds(0)

            If you only use k one time, I would probably just put the literal string in the .OpenRecordset() call.

eusebio June 2nd, 2005 07:33 AM

Brian, thanks for the sugestions.

The question about working easily with queries wasn't about writing them, cause that's not a problem but effectivelly working with data controls, data sets ... to help me make better updates/inserts.
I like to have full control of my data so first I will store all my values to variables end after that insert/update them in the database.
I'll send you some code examples so that you can make an idea what I'm talking about ... when I get home, I'm at work now ...

Thanks,
Eusebio.


sugar March 30th, 2006 07:41 PM

hi, cound you please help with this topic? its almost the samething presented here, but im stuck...
http://p2p.wrox.com/topic.asp?TOPIC_ID=42205

Thanks



All times are GMT -4. The time now is 01:18 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.