Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

View Poll Results: Was this question clear?
Very Clear 1 100.00%
Clear enough 0 0%
Required a bit more info 0 0%
As clear as Mud?! 0 0%
Voters: 1. You may not vote on this poll

Reply
 
Thread Tools Display Modes
Old Mar 7th, 2005, 9:06 AM   #1
eggsy
Newbie
 
Join Date: Dec 2004
Posts: 5
Rep Power: 0 eggsy is on a distinguished road
Question Embedded SQL queries

Hi Guys

Just wondering if anyone can help?

Suppose I have two tables in Access

Customers
===========
CustID
CustName


Reservations
===========
ResID
CustID

As you can see the relationship between the two is CustID.

Now if I have a form and two dataControls :

datCustomers and datReservations and each corrosponding to the given table / record source

How could I code a button that when clicked runs the SQL query given below:

SELECT c.CustomerID, c.CustomerName
FROM CustomerDetails c, reservationDetails r
WHERE r.CustomerID = c.CustomerID

Any other info needed ?

Regards all and thank you for reading

Eggsy

Last edited by eggsy; Mar 7th, 2005 at 9:15 AM. Reason: Tables described incorrectly
eggsy is offline   Reply With Quote
Old Mar 7th, 2005, 10:12 AM   #2
faLLeN
Newbie
 
Join Date: Mar 2005
Location: Hertfordshire, UK
Posts: 16
Rep Power: 0 faLLeN is on a distinguished road
Send a message via MSN to faLLeN
Cool

I normally steer clear of using the data controls. I would use ADO 2.8 as a reference and create the objects myself. I've found that to be the most flexible way to play with data really.

If for example those tables where in Microsoft SQL Server, I would use the below code:

Option Explicit

Dim dbConnection    As ADODB.Connection
Dim dbRecordset     As ADODB.Recordset

Public Sub ShowData()

    'This just outputs your query in the immediate pane (press Ctrl+G to show it)
  
    Dim strConnectionString As String
    Dim strSQLQuery         As String
    Dim strUsername         As String
    Dim strPassword         As String
    Dim strDatabase         As String
    Dim strServer           As String
    Dim intIndexRef         As Integer
    
    Set dbConnection = New ADODB.Connection
    Set dbRecordset = New ADODB.Recordset
    
    'Setup db details
    strUsername = "username"
    strPassword = "password"
    strDatabase = "database"
    strServer = "SQL-SERVER-HOST"
    strSQLQuery = "SELECT c.CustomerID, c.CustomerName " & _
                  "FROM CustomerDetails c, reservationDetails r " & _
                  "WHERE r.CustomerID = c.CustomerID"
    
    'define connection string
    strConnectionString = "Provider=SQLOLEDB.1;" & _
                          "Password=" & strPassword & ";" & _
                          "User ID=" & strUsername & ";" & _
                          "Initial Catalog=" & strDatabase & ";" & _
                          "Data Source=" & strServer
    
    'connect
    dbConnection.ConnectionString = strConnectionString
    dbConnection.Open
    
    With dbRecordset
        
        'perform query
        .Open strSQLQuery, dbConnection
    
        dbRecordset.MoveFirst
    
        'loop through all returned rows
        Do
            
            'print out all the field names and thier values
            For intIndexRef = 0 To (dbRecordset.Fields.Count - 1)
                
                Debug.Print .Fields(intIndexRef).Name & ": " & _
                            .Fields(intIndexRef).Value
                
            Next
            
            Debug.Print ""
            
            dbRecordset.MoveNext
            
        Loop Until (.EOF)
    
    End With
    
    'close recordset
    dbRecordset.Close
    
    'disconnect
    dbConnection.Close
    
    Set dbRecordset = Nothing
    Set dbConnection = Nothing

End Sub

Edit:
Sos.. just noticed you wanted MS Access.

Just use a different connection string, something like this:

(put the filename to the MDB in strDatabase and dont bother with strServer)
strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ 
                      "UserID=" & strUsername & ";" & _ 
                      "Password=" & strPassword & ";" & _
                      "Data Source=" & strDatabase

Hope that helps.

Let me know

Matthew Hall
www.indarkness.co.uk

Last edited by faLLeN; Mar 7th, 2005 at 10:22 AM.
faLLeN is offline   Reply With Quote
Old Mar 7th, 2005, 1:40 PM   #3
eggsy
Newbie
 
Join Date: Dec 2004
Posts: 5
Rep Power: 0 eggsy is on a distinguished road
Great post mate will get on coding.

Sorry to drag more out of you, but after performing above what would the code be to display the results in a textField?
eggsy is offline   Reply With Quote
Old Mar 7th, 2005, 5:40 PM   #4
faLLeN
Newbie
 
Join Date: Mar 2005
Location: Hertfordshire, UK
Posts: 16
Rep Power: 0 faLLeN is on a distinguished road
Send a message via MSN to faLLeN
Cool Adding to a text field

Hi

Well adding to a text field isnt exactly fitting for the purpose, you should really stick the returned rows into a ListView control (or similar).

But, as you wish, you can do the following.

1) Add this sub:

Private Sub AddItem (strText As String)

   'Adds strText to a multiline textbox control 
   txtTextBox.MultiLine = True
   txtTextBox.Scrollbars = 2 '(Vertical scrollbar) MS didnt provide any Enum for this.. helpful.. :S

   txtTextBox.Text = (txtTextBox.Text & strText & vbCrLf)

End Sub

2) Replace 'Debug.Print' with 'AddItem' in the previous code example I gave.

You'll need a text control called txtTextBox in current scope of the code module you added.

Hey presto, your textbox now shows that data output!

Kind Regards,
Matthew Hall
www.indarkness.co.uk
www.idhosting.co.uk
faLLeN is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC