Solved

Convert SQL into  VB6 Function

Posted on 2007-04-11
6
208 Views
Last Modified: 2013-12-25
Hope this makes sense;  The following is a piece of SQL code.  
 
PARAMETERS [Enter MovieID:] Long;
SELECT DISTINCT m.[MovieID], m.[MovieName]
FROM Movies AS m INNER JOIN [SELECT [Actor1] AS [Actor] FROM Movies WHERE [MovieID] = [Enter MovieID:] UNION SELECT [Actor2] AS [Actor] FROM Movies WHERE [MovieID] = [Enter MovieID:] UNION SELECT [Actor3] AS [Actor] FROM Movies WHERE [MovieID] = [Enter MovieID:] UNION SELECT [Actor4] AS [Actor] FROM Movies WHERE [MovieID] = [Enter MovieID:] UNION SELECT [Actor5] AS [Actor] FROM Movies WHERE [MovieID] = [Enter MovieID:]]. AS u ON m.[Actor1] = u.[Actor] OR m.[Actor2] = u.[Actor] OR m.[Actor3] = u.[Actor] OR m.[Actor4] = u.[Actor] OR m.[Actor5] = u.[Actor];

It is a parameter select query which asks for the value 'MovieID' and then performs the query.    I need to convert the code into a VB6 Function which requires the input value (a number representing MovieID) and then returns the list of movies - just like the above SQL.

Anybody know how to do this?
0
Comment
Question by:wjshore
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 11

Expert Comment

by:Sven
ID: 18888881
Something like this?

Function GetMovieSQL(byVal MovieID)

GetMovieSQL = "SELECT DISTINCT m.[MovieID], m.[MovieName] FROM Movies AS m INNER JOIN [SELECT [Actor1] AS [Actor] FROM Movies WHERE [MovieID] = " & MovieID & " UNION SELECT [Actor2] AS [Actor] FROM Movies WHERE [MovieID] = " & MovieID & " UNION SELECT [Actor3] AS [Actor] FROM Movies WHERE [MovieID] = [Enter MovieID:] UNION SELECT [Actor4] AS [Actor] FROM Movies WHERE [MovieID] = " & MovieID  & " UNION SELECT [Actor5] AS [Actor] FROM Movies WHERE [MovieID] = " & MovieID & "] AS u ON m.[Actor1] = u.[Actor] OR m.[Actor2] = u.[Actor] OR m.[Actor3] = u.[Actor] OR m.[Actor4] = u.[Actor] OR m.[Actor5] = u.[Actor]"

End Function

And then the rest of the code has to be somthing klike this (in short):

1. Open a SQL connection
2. Open a recordset: rs.open GetMovieSQL(1014) ...
3. do whatever you want
4. Close recordset and connection
0
 

Author Comment

by:wjshore
ID: 18888990
Thanks for trying.  Code won't compile.  Entering as MovieID as Call GetMovieSQL(13), where 13 is a valid ID, produces nothing.
0
 
LVL 11

Expert Comment

by:Sven
ID: 18889045
"Call GetMovieSQL(13)" will not return anything!

Should be something like: "strSQL = GetMovieSQL(13)"

Then strSQL will be the SQL syntax to call by recordset.

You can check this with "MsgBox strSQL" or sth. like that.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:wjshore
ID: 18893458
Darth:
Ease up on the exclamation points.  Makes you seem as though your light sabre's too short.  That said, it still appears we're not communicating.  Your function consists entirely of a string called GetMovieSQL.  How is it called?  You suggest another string called strSQL.  Can you please show all the code which allows the SQL to return a recordset with the input value of MovieID?   If I knew how, I wouldn't have asked.  Thanks,

Warren Shore
0
 
LVL 11

Accepted Solution

by:
Sven earned 500 total points
ID: 18896048
I thought you know the basics of programming some simple recordsets ;)

Dim objConn as ADODB.Connection
Dim objRs as  ADODB.Recordset
Dim ConnectionString as String

ConnectionString = "Data Provider=MSDASQL;Driver={SQL Server};Server=MySQLServerHostOrIP;UID=USER;PWD=PASSWORD;DATABASE=NAMEOFDATABASE"
   
Set objConn = New ADODB.Connection
   
    With objConn
        .Provider = "MSDataShape"
        .ConnectionTimeout = 15
        .CommandTimeout = 1200
        .Open ConnectionString
    End With

    Set objRs = New ADODB.Recordset
    objRs.Open GetMovieSQL(13), objConn, adOpenStatic, adLockOptimistic

    MsgBox objRs("MovieName")

    objRs.Close
    Set objRs = Nothing

objConn.Close
Set objConn = Nothing
0
 
LVL 11

Expert Comment

by:Sven
ID: 18896053
.ConnectionTimeout is the timeout to establish the connection to database.
.CommandTimeout is the timeout in seconds to call the sql statement.
Recordset will be opend ReadOnly.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Add and format columns in vb6 7 84
fso.FolderExists("\\server\HiddenFolder$") 4 96
MS SQL Update query with connected table data 3 80
Problem to line 23 80
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question