Results 1 to 17 of 17

Thread: Convert VB to VBA

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Convert VB to VBA

    How would this procedure be converted to VBA?
    Code:
    Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" _
        (ByVal hwndParent As Long, ByVal fRequest As Long, _
        ByVal lpszDriver As String, ByVal lpszAttributes As String) As Long
        Public DB As ADODB.Connection
        Public connString As String
     
    Public Sub Connect()
        Call Create
        
        connString = "DSN=VRRS;"
        Set DB = New ADODB.Connection
        DB.Open connString
    End Sub
     
    Private Function CreateAccessDSN(DSNName As String, DatabaseFullPath As String) As Boolean
        Dim sAttributes As String
        sAttributes = "DSN=" & DSNName & Chr(0)
        sAttributes = sAttributes & "DBQ=" & DatabaseFullPath & Chr(0)
        CreateAccessDSN = CreateDSN("Microsoft Access Driver (*.mdb)", sAttributes)
    End Function
     
    Private Function CreateDSN(Driver As String, Attributes As String) As Boolean
        CreateDSN = SQLConfigDataSource(0&, 1, Driver, Attributes)
    End Function
     
    Private Sub Create()
        Dim blnRetVal As Boolean
        blnRetVal = CreateAccessDSN("VRRS", "C:\VRRS\VRRS.mdb")
    End Sub
    What I am trying to do is use VBA to set up a DSN on my PC
    Last edited by Jo15765; May 3rd, 2013 at 01:28 PM.

  2. #2
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Convert VB to VBA

    Jo

    If you use exactly that code as a macro on an Excel sheet (ie, VBA) ,,,
    .. does it work?
    .. does it crash? If so, at what line?

    Spoo

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: Convert VB to VBA

    I could be totally wrong, but it seems to me this was creating an ACCESS DSN, and I am needing a SQL Server DSN. However, to answer your questions, the Compile errors I get is:
    Compile Error:
    User-Defined type not defined
    and these are the 2 lines that produces that error:
    Code:
        Public DB As ADODB.Connection
        Set DB = New ADODB.Connection

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Convert VB to VBA

    add a reference to ADO
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: Convert VB to VBA

    Quote Originally Posted by westconn1 View Post
    add a reference to ADO
    I added a reference to Microsoft ADO Ext. 6.0 for DDL and Security maybe that's the wrong one?

    EDIT -- and it is Excel 2007
    Last edited by Jo15765; May 3rd, 2013 at 07:25 PM. Reason: Added ver of Excel

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Convert VB to VBA

    i guess i would be using ADO ver 2.8 library, but get confused with the different versions used for later versions of windows
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: Convert VB to VBA

    Correct me if I am wrong, but I am looking for the Object Library right?

    If that statement is true, I do not have a ADO object library available as a reference...I only have the DAO
    Microsoft DAO 3.6 Object Library

  8. #8
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Convert VB to VBA

    Jo

    DAO is an older Access feature, but still might work.
    I still used DAO for VB6 apps (. .) ,, but have not tried it in VBA.

    Spoo

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: Convert VB to VBA

    How would the code be converted from ADO to DAO?

  10. #10
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Convert VB to VBA

    Jo

    I'm not familiar with a DSN, so I can't help you there.

    If you are only trying use Excel to open and read from Access, then maybe
    I might be able to help. I'll await your reply.

    Spoo

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: Convert VB to VBA

    I am wanting Excel to create a DSN for SQL Server.

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Convert VB to VBA

    add a reference to ADO
    microsoft activex data objects, any system from win 98 should have available, maybe different in w7 or w8

    afaik you do not have to create a DSN, just use the correct connection string, check out connectionstrings.com
    the code to create a DSN should work when you have a reference to ADO
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: Convert VB to VBA

    So if I just create a connection within the workbook, I will not have to create a system DSN to allow the users to be able to refresh the queries in the workbook?

  14. #14
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Convert VB to VBA

    Jo

    What is a system DSN?

    To just read an Access mdb, perhaps something like this

    Code:
    Sub ReadMyAccess()
        '
        Dim DB1 As Database
        Dim RS1 As Recordset
        '
        mymdb = "c:\access\SomeMDB.mdb"
        myrs = "SomeRS"
        '
        Set DB1 = OpenDatabase(mymdb)
        Set RS1 = DB1.OpenRecordset(myrs)
        '
        v1 = RS1.RecordCount
        RS1.MoveFirst
        For ii = 1 To 6
            v2 = RS1("date")
            v3 = RS1("day")
            RS1.MoveNext
        Next ii
    End Sub
    I added these Tools>References

    ,, Microsoft Access 11.0 Object Library
    ,, Microsoft DAO 3.6 Object Library

    Spoo

  15. #15
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Convert VB to VBA

    I added these Tools>References

    ,, Microsoft Access 11.0 Object Library
    ,, Microsoft DAO 3.6 Object Library
    the first would require access to be installed and DAO should be avoided with any other database than access, even with access it is better to use ADO now
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  16. #16
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Convert VB to VBA

    the first would require access to be installed and DAO should be avoided with any other database than access, even with access it is better to use ADO now
    Agreed, but OP indicated that an ADO reference was not an available reference.
    So, are you suggesting dropping the first one and just keeping the second?

  17. #17
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Convert VB to VBA

    So, are you suggesting dropping the first one and just keeping the second?
    no, the first may not be available if access is not installed and he indicated it was not an access database, but sql server

    i suggest he finds why ADO is not available, if he is using w7 or w8 x64, it may well be a different driver
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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