Results 1 to 12 of 12

Thread: Creating a Connection String

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Creating a Connection String

    I was wondering if I could create a connection string with a function, so that I can call the function once, instead of creating connections within each function. Can anyone let me know if this is possible?

    VB Code:
    1. Private Function connectToDB(database as String) as ADODB.Connection
    2.      Dim connection as ADODB.connection    
    3.      Set connection = New ADODB.connection
    4.      connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    5.                "Data Source=" & database
    6.      connection.Open
    7.      connectToDB = connection
    8. End Function

    Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Creating a Connection String

    If you'd try it, you might find that it works.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: Creating a Connection String

    Well I keep getting a user defined datatype error. Is there something I need to add in the references or how do I go about defining a datatype?

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Creating a Connection String

    On what line to do you get the error (as tg intimated, on the surface, it looks just fine)?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: Creating a Connection String

    It's complaining that ADODB.Connection is not a return type, and is a user defined type

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Creating a Connection String

    Why don't you just use 1 public connection object?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Creating a Connection String

    Did you set a reference to ADO?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: Creating a Connection String

    OK so I'm still working on creating a function that inputs a database name and outputs a connection to that database. Currently I am getting a "Run-time error '91': Object variable or With block variable not set" whenever I try to return the connection (i.e. connect = connection). Any help would be greatly appreciated. Thanks

    VB Code:
    1. Private Sub Form_Load()
    2.     Dim conn As ADODB.connection
    3.     conn = connect("Test")
    4.     Dim sql As String
    5.     sql = "SELECT * FROM [TEST]"
    6.     conn.Execute sql
    7.  
    8.     Debug.Print sql
    9. End Sub
    10.  
    11. Private Function connect(database As String) As ADODB.connection
    12.     Dim connection As ADODB.connection
    13.     Set connection = New ADODB.connection
    14.     connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    15.        "Data Source= C:\Documents and Settings\P6B0438\My Documents\Test\" & _
    16.        database & ".mdb"
    17.     connection.Open
    18.  
    19.     connect = connection
    20.    
    21. End Function

    Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: Creating a Connection String

    bump

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Creating a Connection String

    As it is an object variable you need to use Set, eg:
    VB Code:
    1. Set connect = connection
    You should also clear the memory used by the local variable, eg:
    VB Code:
    1. Set connection = Nothing

  11. #11
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Creating a Connection String

    There are several things that you need to change.

    First, your connection object declaration, as it is, is confined to your Function and is not available outside of your function (I'm guessing that is your object not set.) Move your these declares to the Form's declaration section so they they will be available throughout your form.
    VB Code:
    1. Option Explicit
    2.  
    3. Private conn As ADODB.Connection
    4. Private adoRS As ADODB.Recordset
    5. Private sSQL As String
    Second, you Execute action queries such as INSERT, UPDATE, DELETE etc. SELECT statements create recordsets. They are not executed. The code for this would be
    VB Code:
    1. Set adoRS = NEW ADODB.Recordset
    2.  
    3. sSQL = "SELECT * FROM [TEST]"
    4. adoRS.Open sSQL, conn

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: Creating a Connection String

    Thanks did not know that about when to use the execute command

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width