Solved

Convert SQL into  VB6 Function

Posted on 2007-04-11
6
204 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

947 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now