Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Pro VB Databases
|
Pro VB Databases Advanced-level VB coding questions specific to using VB with databases. Beginning-level questions or issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 28th, 2005, 02:54 PM
Registered User
 
Join Date: May 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to eusebio
Default 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.
 
Old May 30th, 2005, 12:41 AM
Authorized User
 
Join Date: Dec 2004
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Eusebio,

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

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



 
Old May 31st, 2005, 11:54 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

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
 
Old June 1st, 2005, 02:10 PM
Registered User
 
Join Date: May 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to eusebio
Default

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.

 
Old June 1st, 2005, 05:16 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

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.
 
Old June 2nd, 2005, 07:33 AM
Registered User
 
Join Date: May 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to eusebio
Default

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.

 
Old March 30th, 2006, 07:41 PM
Authorized User
 
Join Date: Mar 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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






Similar Threads
Thread Thread Starter Forum Replies Last Post
get the execute query result in variable mahen_pali SQL Server 2005 2 April 7th, 2008 04:04 AM
Query Result to Session Variable johnp ASP.NET 2.0 Basics 1 April 6th, 2006 11:15 PM
OTHER: Pass query result to a Form sugar Pro VB Databases 3 April 4th, 2006 11:14 AM
Pass variable to linked server query sofya SQL Server 2000 4 November 23rd, 2004 02:38 PM
Setting a Variable = Result of a SQL Query PeteS VB Databases Basics 1 June 20th, 2003 08:14 AM





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