|
-
May 3rd, 2013, 01:17 PM
#1
Thread Starter
Hyperactive Member
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.
-
May 3rd, 2013, 04:19 PM
#2
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
-
May 3rd, 2013, 06:04 PM
#3
Thread Starter
Hyperactive Member
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
-
May 3rd, 2013, 06:50 PM
#4
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
-
May 3rd, 2013, 06:54 PM
#5
Thread Starter
Hyperactive Member
Re: Convert VB to VBA
 Originally Posted by westconn1
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
-
May 4th, 2013, 03:55 AM
#6
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
-
May 4th, 2013, 08:18 AM
#7
Thread Starter
Hyperactive Member
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
-
May 4th, 2013, 12:40 PM
#8
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
-
May 4th, 2013, 02:31 PM
#9
Thread Starter
Hyperactive Member
Re: Convert VB to VBA
How would the code be converted from ADO to DAO?
-
May 4th, 2013, 03:35 PM
#10
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
-
May 4th, 2013, 04:06 PM
#11
Thread Starter
Hyperactive Member
Re: Convert VB to VBA
I am wanting Excel to create a DSN for SQL Server.
-
May 4th, 2013, 05:53 PM
#12
Re: Convert VB to VBA
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
-
May 4th, 2013, 07:36 PM
#13
Thread Starter
Hyperactive Member
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?
-
May 4th, 2013, 10:37 PM
#14
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
-
May 4th, 2013, 10:55 PM
#15
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
-
May 5th, 2013, 07:37 AM
#16
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?
-
May 5th, 2013, 04:17 PM
#17
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|