Sub datashape() Dim cnn As New ADODB.Connection Dim rst As New ADODB.Recordset Dim rsChapter As Variant cnn.Provider = "MSDataShape" cnn.Open "Data Provider=MSDASQL;" & _ "DSN=vfox;uid=sa;pwd=vfox;database=pubs” '步骤 1 rst.StayInSync = FALSE rst.Open "SHAPE {select * from authors} APPEND ({select * from titleauthor} AS chapter RELATE au_id TO au_id)", cnn '步骤 2 While Not rst.EOF Debug.Print rst("au_fname"), rst("au_lname"), rst("state"), rst("au_id") '步骤 3 Set rsChapter = rst("chapter") '步骤 4 While Not rsChapter.EOF Debug.Print rsChapter(0), rsChapter(1), rsChapter(2), rsChapter(3) rsChapter.MoveNext Wend rst.MoveNext Wend End Sub
Public Function ExecuteSQL(ByVal sql _ As String, MsgString As String) _ As ADODB.Recordset
ConnectString = "DBQ=" & DataBaseName & ";pwd=cjh;DefaultDir=;Driver={Microsoft Access Driver (*.mdb)};" Dim cnn As ADODB.Connection Dim rst As ADODB.Recordset Dim sTokens() As String
On Error GoTo ExecuteSQL_Error
sTokens = Split(sql) Set cnn = New ADODB.Connection cnn.Open ConnectString If InStr("INSERT,DELETE,UPDATE", _ UCase$(sTokens(0))) Then cnn.Execute sql MsgString = sTokens(0) & _ " query successful" Else Set rst = New ADODB.Recordset rst.Open Trim$(sql), cnn, _ adOpenKeyset, _ adLockOptimistic Set ExecuteSQL = rst End If Set rst = Nothing Set cnn = Nothing Exit Function ExecuteSQL_Exit: Set rst = Nothing Set cnn = Nothing Exit Function
ExecuteSQL_Error: Resume ExecuteSQL_Exit End Function以上不知能不能帮到你!
Public adoCn As New ADODB.Connection Public adoRs As New ADODB.Recordset '------------------------------连接数据库----------------------------------- Public Function Connection() As Integer Dim sCon As String On Error GoTo Errs sCon = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=密码;Data Source=" + App.Path + "\db1.mdb" adoCn.CursorLocation = adUseClient adoCn.Open sCon Connection = 0 Exit Function Errs: Connection = -1 End Function '-----------------------------断开数据库---------------------------------- Public Function DisConnection() As Integer On Error GoTo Errs If adoCn.State = adStateOpen Then adoCn.Close Set adoCn = Nothing DisConnection = 0 Exit Function Errs: DisConnection = -1 End Function
这些代码很好,可我要的是如何写Shape Recordset 的代码呀~~"SHAPE {select * from authors} APPEND ({select * from titleauthor} AS chapter RELATE au_id TO au_id)" 这句话可以返回一个分层的Recordset 第一个Recordset 里面除了*里有的域之外多出来一个chapter域,里面包含着另外一个表 我是用这个主要想通过data report来实现有明细打印,即以下的样子 姓名 allan 第一项 第二项 第三项 a b c a2 b2 c2 a3 b3 c3 姓名 BBBBB 第一项 第二项 第三项 a4 b4 c4 a5 b5 c5 a6 b6 c6有了分层的recordset 直接就能绑定了,在dataEvironment里能做到,我想,用代码也应该能 现在的问题是如果我写 cnn.Provider = "MSDataShape" cnn.Open "Data Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB" & _ "DSN=vfox;uid=sa;pwd=vfox;database=pubs” 提示connectstring出错,怎么办? 或者有其他的好办法??不想用复杂的Crystal 谢谢了,分不够的话,我的另外一样的一贴被埋在下面,分都给!http://expert.csdn.net/Expert/topic/2330/2330268.xml?temp=5.921572E-02
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim rsChapter As Variant cnn.Provider = "MSDataShape"
cnn.Open "Data Provider=MSDASQL;" & _
"DSN=vfox;uid=sa;pwd=vfox;database=pubs”
'步骤 1
rst.StayInSync = FALSE
rst.Open "SHAPE {select * from authors}
APPEND ({select * from titleauthor} AS chapter
RELATE au_id TO au_id)",
cnn
'步骤 2
While Not rst.EOF
Debug.Print rst("au_fname"), rst("au_lname"),
rst("state"), rst("au_id")
'步骤 3
Set rsChapter = rst("chapter")
'步骤 4
While Not rsChapter.EOF
Debug.Print rsChapter(0), rsChapter(1),
rsChapter(2), rsChapter(3)
rsChapter.MoveNext
Wend
rst.MoveNext
Wend
End Sub
As String, MsgString As String) _
As ADODB.Recordset
ConnectString = "DBQ=" & DataBaseName & ";pwd=cjh;DefaultDir=;Driver={Microsoft Access Driver (*.mdb)};"
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo ExecuteSQL_Error
sTokens = Split(sql)
Set cnn = New ADODB.Connection
cnn.Open ConnectString
If InStr("INSERT,DELETE,UPDATE", _
UCase$(sTokens(0))) Then
cnn.Execute sql
MsgString = sTokens(0) & _
" query successful"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(sql), cnn, _
adOpenKeyset, _
adLockOptimistic
Set ExecuteSQL = rst
End If
Set rst = Nothing
Set cnn = Nothing
Exit Function
ExecuteSQL_Exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function
ExecuteSQL_Error:
Resume ExecuteSQL_Exit
End Function以上不知能不能帮到你!
Public adoRs As New ADODB.Recordset
'------------------------------连接数据库-----------------------------------
Public Function Connection() As Integer
Dim sCon As String
On Error GoTo Errs
sCon = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=密码;Data Source=" + App.Path + "\db1.mdb"
adoCn.CursorLocation = adUseClient
adoCn.Open sCon
Connection = 0
Exit Function
Errs:
Connection = -1
End Function
'-----------------------------断开数据库----------------------------------
Public Function DisConnection() As Integer
On Error GoTo Errs
If adoCn.State = adStateOpen Then adoCn.Close
Set adoCn = Nothing
DisConnection = 0
Exit Function
Errs:
DisConnection = -1
End Function
APPEND ({select * from titleauthor} AS chapter
RELATE au_id TO au_id)"
这句话可以返回一个分层的Recordset
第一个Recordset 里面除了*里有的域之外多出来一个chapter域,里面包含着另外一个表
我是用这个主要想通过data report来实现有明细打印,即以下的样子
姓名 allan
第一项 第二项 第三项
a b c
a2 b2 c2
a3 b3 c3
姓名 BBBBB
第一项 第二项 第三项
a4 b4 c4
a5 b5 c5
a6 b6 c6有了分层的recordset 直接就能绑定了,在dataEvironment里能做到,我想,用代码也应该能
现在的问题是如果我写
cnn.Provider = "MSDataShape"
cnn.Open "Data Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB" & _
"DSN=vfox;uid=sa;pwd=vfox;database=pubs”
提示connectstring出错,怎么办?
或者有其他的好办法??不想用复杂的Crystal
谢谢了,分不够的话,我的另外一样的一贴被埋在下面,分都给!http://expert.csdn.net/Expert/topic/2330/2330268.xml?temp=5.921572E-02