急用 如何用代码使DataGrid控件链接到数据库?
Public Function ExcuteSQL(sql As String) As String
Dim mycommection As Connection
Dim myrecordset As Recordset
'连接数据库
Set myconnection = New Connection
Set myrecordset = New Recordset
mypath = App.Path & "\mis.mdb"
myconnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & mypath
myrecordset.Open "[communication]", myconnection, adOpenKeyset, adLockOptimistic
Set ExcuteSQL = myrecordset
End Function
Private Sub cmdok_Click()
Dim sql As String
spl = "select * from communication"
Set DataGrid1.DataSource = myrecordset
DataGrid1.Refresh
End Sub
以上代码那里有问题?
请各位高手指点。
问题点数:0、回复次数:6Top
1 楼zcm123(老蝌蚪精 ●~ www.84ren.com 来了就下的源码站)回复于 2003-12-01 20:12:47 得分 0
myconnection.CursorLocation =adUseClient
加个这个再式一试Top
2 楼tjl713(tjl)回复于 2003-12-01 21:27:00 得分 0
在通用申明里写上:
Public myCon As new ADODB.Recordset '用于打开连接
Public myRes As new ADODB.Connection '用于打开记录
你的 Dim mycommection As Connection
Dim myrecordset As Recordset
'连接数据库
Set myconnection = New Connection
Set myrecordset = New Recordset
这几条语句就不要了!!!
下面你还要自己改成myCon.Open等,你自己看着办!
关键是cmdok_Click()的事件,你要调用你的ExcuteSQL函数。
Private Sub cmdok_Click()
Dim sql As String
sql = "select * from communication"
ExcuteSQL(sql)
Set DataGrid1.DataSource = myrecordset
DataGrid1.Refresh
End Sub
Top
3 楼hzybc(网友帮帮忙;帮帮网友忙)回复于 2003-12-01 21:59:51 得分 0
Set ExcuteSQL = myrecordset 这句改成
Set myrecordset = ExcuteSQL(sql)Top
4 楼silverblade(银色刀刃)回复于 2003-12-01 22:38:06 得分 0
Public Function ExcuteSQL(sql As String) As String
你的函数声明中声明函数返回值是STRING
可返回的却是个RECORDSET
你说会对吗?Top
5 楼online(龙卷风V4.0--决战江湖(MS MVP-VB))回复于 2003-12-02 00:16:37 得分 0
贴一段测试过的
Option Explicit
Private conn As ADODB.Connection
Private rs As ADODB.Recordset
Private Sub Form_Load()
Dim apppath As String
Dim DbFileName As String
Dim ConnectString As String
Dim i As Integer
Set conn = New ADODB.Connection
If Right(App.Path, 1) = "\" Then
apppath = App.Path
Else
apppath = App.Path & "\"
End If
DbFileName = apppath & "article.mdb"
ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
DbFileName & ";Persist Security Info=False;"
On Error Resume Next
With conn
.CursorLocation = adUseClient
.Open ConnectString
End With
Set rs = New ADODB.Recordset
rs.Open "select id,mc from mz", conn, 1, 3
If rs.EOF Then
Exit Sub
End If
Set DataGrid1.DataSource = rs
End SubTop
6 楼online(龙卷风V4.0--决战江湖(MS MVP-VB))回复于 2003-12-02 00:24:42 得分 0
Public Function ExcuteSQL(sql As String) As String
你的函数声明中声明函数返回值是STRING
可返回的却是个RECORDSET
改正后的代码
Option Explicit
Private conn As adodb.Connection
Private rs As adodb.Recordset
Private Function ExcuteSQL(sql As String) As adodb.Recordset
Dim apppath As String
Dim DbFileName As String
Dim ConnectString As String
Dim i As Integer
Set conn = New adodb.Connection
If Right(App.Path, 1) = "\" Then
apppath = App.Path
Else
apppath = App.Path & "\"
End If
DbFileName = apppath & "article.mdb"
ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
DbFileName & ";Persist Security Info=False;"
On Error Resume Next
With conn
.CursorLocation = adUseClient
.Open ConnectString
End With
Set rs = New adodb.Recordset
rs.Open sql, conn, 1, 3
If rs.EOF Then
Exit Function
End If
Set ExcuteSQL = rs
End Function
Private Sub Command1_Click()
Dim sql As String
sql = "select id,mc from mz"
Set DataGrid1.DataSource = ExcuteSQL(sql)
DataGrid1.Refresh
End Sub
Top




