List SQLServerAvailable

  • Dear Sir/Madam...

    please help to corect my program

    here is the code for my modul

    Public Function AvailableSQLServers() As String()

    Dim oServer As New SQLDMO.Application

    Dim oNameList As SQLDMO.NameList

    Dim iElement As Integer

    Dim sAns() As String

    Dim lCtr As Long, lCount As Long

    On Error GoTo ErrorHandler

    ReDim sAns(0) As String

    Set oNameList = oServer.ListAvailableSQLServers

    With oNameList

    lCount = .Count

    If lCount > 0 Then

    For lCtr = 1 To .Count

    iElement = IIf(sAns(0) = "", 0, UBound(sAns) + 1)

    ReDim Preserve sAns(iElement) As String

    sAns(iElement) = oNameList.Item(lCtr)

    Next

    End If

    End With

    AvailableSQLServers = sAns

    Exit Function

    ErrorHandler:

    ReDim sAns(0) As String

    AvailableSQLServers = sAns

    End Function

    And This is my code for my UserLogin Form

    Option Explicit

    Private mApplication As New SQLDMO.Application

    Private mSQLServer As New SQLDMO.SQLServer

    Private myDB As New SQLDMO.Database

    Private mConn As Boolean

    Private Sub Combo1_DropDown()

    Dim sServers() As String

    Dim iCtr As Integer

    sServers = AvailableSQLServers

    If sServers(0) = "" Then

    MsgBox "Komputer Ini Tidak Mempunyai Koneksi Ke Server " & Chr(13) & _

    "Program Aplikasi Akan Ditutup", vbCritical, "Kesalahan Koeneksi"

    ' End

    Else

    For iCtr = 0 To UBound(sServers)

    Combo1.AddItem sServers(iCtr)

    'Debug.Print sServers(iCtr)

    Next

    End If

    End Sub

    Private Sub Command1_Click()

    mSQLServer.Disconnect

    Set myDB = Nothing

    End

    End Sub

    Private Sub Command2_Click()

    Dim Pesan, cari As String

    Dim i, j As Integer

    On Error GoTo ErrorHandler

    Pesan = "User : " & Text1.Text & " Failed to connect to Server" & Combo1.Text & " "

    Pesan = Pesan & Chr(13) & "Lease Check your User Name and Password "

    ServerName = Trim(Combo1.Text)

    Uname = Trim(Text1.Text)

    Pass = Trim(Text2.Text)

    mSQLServer.ApplicationName = "List Server Available"

    If mConn = False Then

    If ((Combo1.Text = "") Or (Combo1.Text = ".")) Then

    MsgBox "Please select one of SQLServer Available", vbInformation, "SQLServer Name Confirmation"

    Combo1.SetFocus

    ElseIf Text1.Text = "" Then

    MsgBox "You must suply a User ID", vbInformation, "UserID Errorr"

    Else

    mSQLServer.Connect Combo1.Text, Text1.Text, Text2.Text

    mConn = True

    End If

    Me.Hide

    FMenu.Show

    Unload Me

    Command2.Caption = " &DisConnect "

    Else

    Command2.Caption = " &Connect "

    mSQLServer.Disconnect

    Set myDB = Nothing

    End If

    Exit Sub

    ErrorHandler:

    If Err.Number = -2147203048 Then

    MsgBox Pesan, vbCritical, "Connection Error"

    Else

    MsgBox Err.Description

    End If

    Screen.MousePointer = vbDefault

    End Sub

    Private Sub Form_Load()

    mConn = False

    End Sub

    Private Sub Text2_KeyPress(KeyAscii As Integer)

    If KeyAscii = 13 Then

    Command2_Click

    End If

    End Sub

  • Just looking at it now. Here are a couple comments:

    1) If KeyAscii = 13 Then

    Command2_Click

    End If

    You can do this more cleaning by making the command button the default button for the form.

    2) For lCtr = 1 To .Count

    iElement = IIf(sAns(0) = "", 0, UBound(sAns) + 1)

    ReDim Preserve sAns(iElement) As String

    sAns(iElement) = oNameList.Item(lCtr)

    Next

    There is no need to redim each time, you should be able to set the array size before you enter the loop. Preserve is expensive (though in this context you'd never notice the performance hit) and again, you can code it more cleanly.

    3) You've got a few variables undeclared, though maybe they are public in parts you havent posted.

    4) I tested and it populates the combo box with the available servers, given a correct login/password it connects fine.

    So...what problem are you having?

    Andy

    http://www.sqlservercentral.com/columnists/awarren/

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply