Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add or Remove DSN

Databases and tables

Add or Remove DSN

by  craigsboyd  Posted    (Edited  )
Slighthaze = [color blue]NULL[/color]
[img http://www.sweetpotatosoftware.com/ttimages/vfpdsn.gif]

Creating a DSN with VFP couldn't be easier. Cut-n-paste the code into a prg and run from within VFP:

PUBLIC oForm
oForm = CREATEOBJECT("clsvfpdsn")
oForm.show()

DEFINE CLASS clsvfpdsn AS form

Top = 0
Left = 0
Height = 92
Width = 309
DoCreate = .T.
Caption = "VFP CREATED DSN"
WindowType = 1
Name = "Form1"
AutoCenter = .T.

ADD OBJECT command1 AS commandbutton WITH ;
Top = 45, ;
Left = 37, ;
Height = 27, ;
Width = 100, ;
Caption = "Add DSN", ;
Name = "Command1"

ADD OBJECT command2 AS commandbutton WITH ;
Top = 45, ;
Left = 171, ;
Height = 27, ;
Width = 100, ;
Caption = "Remove DSN", ;
Name = "Command2"

ADD OBJECT check1 AS checkbox WITH ;
Top = 19, ;
Left = 37, ;
Height = 17, ;
Width = 82, ;
AutoSize = .T., ;
BackStyle = 0, ;
Caption = "Silent Mode", ;
Value = 1, ;
Name = "Check1"

PROCEDURE Load
DECLARE INTEGER SQLConfigDataSource IN odbccp32;
INTEGER hwndParent,;
INTEGER fRequest,;
STRING lpszDriver,;
STRING lpszAttributes
ENDPROC

PROCEDURE command1.Click
#DEFINE ODBC_ADD_DSN 1
*!* #DEFINE ODBC_CONFIG_DSN 2
*!* #DEFINE ODBC_REMOVE_DSN 3

Local nReturnValue, cDriver, cAttribs

cDriver = "SQL Server"
cAttribs = "SERVER=SomeServer" + Chr(0)
cAttribs = cAttribs + "DESCRIPTION=Temporary DSN created with VFP code" + Chr(0)
cAttribs = cAttribs + "DSN=VFP_CREATED_DSN" + Chr(0)
cAttribs = cAttribs +"DATABASE=pubs" + Chr(0)

IF thisform.check1.value = 0
nReturnValue = SQLConfigDataSource(thisform.hwnd, ODBC_ADD_DSN, cDriver, cAttribs)
ELSE
nReturnValue = SQLConfigDataSource(0, ODBC_ADD_DSN, cDriver, cAttribs)
ENDIF

If nReturnValue = 1
Messagebox("VFP_CREATED_DSN Created",64,"SUCCESS")
Else
Messagebox("Creation of VFP_CREATED_DSN Failed",64,"FAILURE")
Endif
ENDPROC

PROCEDURE command2.Click
#DEFINE ODBC_REMOVE_DSN 3
*!* #DEFINE ODBC_ADD_DSN 1
*!* #DEFINE ODBC_CONFIG_DSN 2

Local nReturnValue, cDriver, cAttribs

cDriver = "SQL Server"
cAttribs = "DSN=VFP_CREATED_DSN" + Chr(0)

IF thisform.check1.value = 0
nReturnValue = SQLConfigDataSource(thisform.hwnd, ODBC_REMOVE_DSN, cDriver, cAttribs)
ELSE
nReturnValue = SQLConfigDataSource(0, ODBC_REMOVE_DSN, cDriver, cAttribs)
ENDIF

If nReturnValue = 1
Messagebox("VFP_CREATED_DSN Deleted",64,"SUCCESS")
Else
Messagebox("Deletion of VFP_CREATED_DSN Failed",64,"FAILURE")
Endif
ENDPROC

ENDDEFINE
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top