Private Sub Command1_Click()
make_connection
g_sql = "select * from aaa" 'aaa為表名
If Not make_i(g_sql) Then
Exit Sub
End If
rs1.MoveFirst
Do While Not rs1.EOF
With MSFlexGrid1
.Row = .Rows - 1
.Col = 0
.Text = rs1("ID")
.Col = 1
.Text = rs1("NAME")
.Col = 2
.Text = IIf(IsNull(rs1("TEL")), "", rs1("TEL")) '這是確保電話為空的時候能顯示出來
.Col = 0
.Sort = 1
End With
Form1.MSFlexGrid1.Rows = Form1.MSFlexGrid1.Rows + 1
rs1.MoveNext
Loop
end sub
所需要調用的模組
Public Function make_connection() As Boolean
db_err = ""
Dim connect_str As String
connect_str = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=username;password=password;"
connect_str = connect_str & "Initial Catalog=pubs;Data Source=***.***.***.***;Connect Timeout=15;Application Name=List" '**為登入SQL所需要的用戶名稱﹐password為密碼﹐時間15秒連接﹐數據庫為pubs﹐上面所綃aaa表就是建立在其中的
On Local Error GoTo connection_error
conn.Open connect_str
make_connection = True
Exit Function
connection_error:
db_err = "Connect fail !! " & Err.Description
make_connection = False
Err.Clear
End Function
Public Function make_i(sql As String) As Boolean
db_err = ""
On Local Error GoTo inq_error
rs1.Open sql, conn, adOpenKeyset, adLockOptimistic, adCmdText
make_i = True
Exit Function
inq_error:
db_err = " Query fail !! " & Err.Description
make_i = False
Err.Clear
End Function