?
Solved

Convert SQL into  VB6 Function

Posted on 2007-04-11
6
Medium Priority
?
210 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses

719 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