Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Pro VB Databases
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 28th, 2005, 02:54 PM
Registered User
 
Join Date: May 2005
Location: , , Romania.
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.
Reply With Quote
  #2 (permalink)  
Old May 30th, 2005, 12:41 AM
Authorized User
Points: 173, Level: 3
Points: 173, Level: 3 Points: 173, Level: 3 Points: 173, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2004
Location: , , .
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>
    ....



Reply With Quote
  #3 (permalink)  
Old May 31st, 2005, 11:54 AM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
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
Reply With Quote
  #4 (permalink)  
Old June 1st, 2005, 02:10 PM
Registered User
 
Join Date: May 2005
Location: , , Romania.
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.

Reply With Quote
  #5 (permalink)  
Old June 1st, 2005, 05:16 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
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.
Reply With Quote
  #6 (permalink)  
Old June 2nd, 2005, 07:33 AM
Registered User
 
Join Date: May 2005
Location: , , Romania.
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.

Reply With Quote
  #7 (permalink)  
Old March 30th, 2006, 07:41 PM
Authorized User
 
Join Date: Mar 2006
Location: Calexico, CA, USA.
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

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 06:27 AM.


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