INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Databases and tables

Add or Remove DSN by craigsboyd
Posted: 25 Aug 03 (Edited 18 Feb 05)

Slighthaze = NULL


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

Back to Microsoft: Visual FoxPro FAQ Index
Back to Microsoft: Visual FoxPro Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close