|
-
Oct 6th, 2000, 07:54 AM
#1
Thread Starter
Fanatic Member
Dear All
Does anybody know if you can create an ODBC datsource through code.
Thanks
Gary Lowe 
VB6 (Enterprise) SP5
ADO 2.6
SQL Server 7 SP3
OK I know my spelling and grammer is crap so don't quote me on it!
To err is human to take the P! is only natural !!
Click on the top section of image for Marcus Miller website and bottom section of image for 'Run For Cover' sound clip

-
Oct 6th, 2000, 10:44 AM
#2
Lively Member
Yes, it is possible. I pulled this code snippet from MSDN. Take a look.
Code:
'Connection Object, Connections Collection Example
'This example demonstrates the Connection object and
'Connections collection by opening a Microsoft Jet Database
'object and two ODBCDirect Connection objects and listing
'the properties available to each object.
Sub ConnectionObjectX()
Dim wrkJet as Workspace
Dim dbsNorthwind As Database
Dim wrkODBC As Workspace
Dim conPubs As Connection
Dim conPubs2 As Connection
Dim conLoop As Connection
Dim prpLoop As Property
' Open Microsoft Jet Database object.
Set wrkJet = CreateWorkspace("NewJetWorkspace", _
"admin", "", dbUseJet)
Set dbsNorthwind = wrkJet.OpenDatabase("Northwind.mdb")
' Create ODBCDirect Workspace object and open Connection
' objects.
Set wrkODBC = CreateWorkspace("NewODBCWorkspace", _
"admin", "", dbUseODBC)
Set conPubs = wrkODBC.OpenConnection("Connection1", , , _
"ODBC;DATABASE=pubs;UID=sa;PWD=;DSN=Publishers")
Set conPubs2 = wrkODBC.OpenConnection("Connection2", , _
True, "ODBC;DATABASE=pubs;UID=sa;PWD=;DSN=Publishers")
Debug.Print "Database properties:"
With dbsNorthwind
' Enumerate Properties collection of Database object.
For Each prpLoop In .Properties
On Error Resume Next
Debug.Print " " & prpLoop.Name & " = " & _
prpLoop.Value
On Error GoTo 0
Next prpLoop
End With
' Enumerate the Connections collection.
For Each conLoop In wrkODBC.Connections
Debug.Print "Connection properties for " & _
conLoop.Name & ":"
With conLoop
' Print property values by explicitly calling each
' Property object; the Connection object does not
' support a Properties collection.
Debug.Print " Connect = " & .Connect
' Property actually returns a Database object.
Debug.Print " Database[.Name] = " & _
.Database.Name
Debug.Print " Name = " & .Name
Debug.Print " QueryTimeout = " & .QueryTimeout
Debug.Print " RecordsAffected = " & _
.RecordsAffected
Debug.Print " StillExecuting = " & _
.StillExecuting
Debug.Print " Transactions = " & .Transactions
Debug.Print " Updatable = " & .Updatable
End With
Next conLoop
dbsNorthwind.Close
conPubs.Close
conPubs2.Close
wrkJet.Close
wrkODBC.Close
End Sub
Dr_Evil
Senior Programmer
VS6 EE
VS.NET EA
-
Oct 6th, 2000, 11:17 AM
#3
Thread Starter
Fanatic Member
Thanks Dr E
But isn't that opening ODBC Datasources, I wanted to know if you can create one.
I want to be able to create an ODBC user datasource for a SQL Server database.
Gary Lowe 
VB6 (Enterprise) SP5
ADO 2.6
SQL Server 7 SP3
OK I know my spelling and grammer is crap so don't quote me on it!
To err is human to take the P! is only natural !!
Click on the top section of image for Marcus Miller website and bottom section of image for 'Run For Cover' sound clip

-
Oct 6th, 2000, 11:39 AM
#4
Hyperactive Member
These are just some examples. I don't know what you are using as your front end. Hope this helps!
HOWTO: Create ODBC Data Sources Using SqlConfigDataSource
Example 1:
* Code to create a new datasource for an Excel file.
*
* First you need to use the Declare DLL function to prototype the
* SQLConfigDataSource function
***
DECLARE Integer SQLConfigDataSource in odbccp32.dll Integer, Integer,;
String, String
***
* Now you need to create a string containing the settings appropriate
* to the driver you are using.. the following is an example for a tier 1
* Microsoft Excel ODBC driver accessing the Schedule.xls file.
***
settings="DSN=NewExcelDataSource"+chr(0)+;
"Description=NewExcel Description"+chr(0)+;
"FileType=Excel 5.0"+chr(0)+;
"DBQ=C:\schedule.xls"+chr(0)+;
"MaxScanRows=16"
* Note: If you have spaces on either side of the equal sign (=), this
* code will not work.
? SQLConfigDataSource(0,1,"Microsoft Excel Driver (*.xls)",settings)
***
Example 2:
* Code to create a new datasource to a VFP table.
*
* First you need to use the Declare DLL function to prototype the
* SQLConfigDataSource function
***
DECLARE Integer SQLConfigDataSource in odbccp32.dll Integer, ;
Integer, String, String
***
* Now you need to create a string containing the settings appropriate
* to the driver you are using. The following is an example for the
* Microsoft VFP ODBC driver accessing the customer.dbf file.
***
settings="DSN=VFP Tables"+chr(0)+;
"Description=VFP ODBC Driver"+chr(0)+;
"SourceDB=c:\vfp\samples\data\customer.dbf"+chr(0)+;
"SourceType=DBF"
* Note: If you have spaces on either side of the equal sign (=), this
* code will not work.
? SQLConfigDataSource(0,1,"Microsoft Visual FoxPro Driver",settings)
***
* Now you can create a view to these datasources either through the view
* designer or in code.
***
-
Oct 6th, 2000, 01:23 PM
#5
Hyperactive Member
set objconn = createObject("ADODB.Connection")
objconn.open "driver={sql server};server=SQLSERVER;database=Logging;uid=administrator;Pwd=beer"
set mycommand = CreateObject("ADODB.Command")
Set mycommand.ActiveConnection = objconn
Set myrs = CreateObject("ADODB.Recordset")
mycommand.commandtext = "Select * from sometable"
myrs.open mycommand
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
|