By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,571 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,571 IT Pros & Developers. It's quick & easy.

odbc create dsn for access using

P: n/a
I'm having trouble using VB.Net to create a system DSN.
I cannot use DSN-less connection, must use ODBC.
I have seen the "Creating odbc dsn with C#".
I have seen many VB examples for creating an dsn for access db, but they seem inconsistent: E.g: Do I use \0 or chr$(0) or chr(0) for the null between attributes? Do I need the null values in the string?
Here is what I have, and it just returns false (or 'zero').

Imports System.Runtime.InteropServices
Public Declare Auto Function SQLConfigDataSource Lib "ODBCCP32.DLL" _
(ByVal hwndParent As Long, ByVal fRequest As Long, _
ByVal lpszDriver As String, ByVal lpszAttributes As String) _
As Integer
End Class

Module CreateDSN

Private Const ODBC_ADD_SYS_DSN = 4 ' Add data source

Public Sub Main()

Dim dbpath as String = "C:\Program Files\AFolder\dbName.mdb"

Dim ret As Integer, Driver as String, Attributes as String

Driver = "Microsoft Access Driver (*.MDB)" & Chr(0)
Attributes = "DSN=" & "Hello" & Chr(0)
Attributes = Attributes & "Uid=Admin" & Chr(0) & "pwd=" & Chr(0)
Attributes = Attributes & "DBQ=" & dbpath & Chr(0) & Chr(0)

ret = ODBCDLL.SQLConfigDataSource(0&, ODBC_ADD_SYS_DSN, Driver, Attributes)
'ret is equal to 1 on success and 0 if there is an error
If ret <> 1 Then
MsgBox("DSN Creation Failed")
End If

End Sub 'Main

End Module

On testing, this always returns 0 (into 'ret'). (It does the same thing when
using \0 instead of Chr(0). I'm not sure how to get any error messages.
Eg: I read we're supposed to use SQLInstallerError but have no idea how to
do that).
I'm not sure what else to try.
So, Any help would be appreciated (especially for VB.NET). Thank you...
Nov 20 '05 #1
Share this Question
Share on Google+
1 Reply

P: n/a

Without looking to the code a long in VBNet is a Int32 maybe you can try
that first mostly than all problems are resolved.

Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.