vb的datareport动态绑定数据源
我要通过在Text1中写入学生姓名,然后单击Command1按钮出现这个学生的信息的报表代码如下PrivateSubForm_Load()DimstrConnectAsS...
我要通过在Text1中写入学生姓名,然后单击Command1按钮 出现这个学生的信息的报表 代码如下
Private Sub Form_Load()
Dim strConnect As String
Dim strProvider As String
Dim strDataSource As String
Dim strDataBaseName As String
Dim pt As String
strProvider = "Provider= Microsoft.Jet.OLEDB.4.0;"
strDataSource = App.Path
strDataBaseName = "\数据库(最新2008-3-13新)zhang82519142.mdb;"
strDataSource = "Data Source=" & strDataSource & _
strDataBaseName
strConnect = strProvider & strDataSource
Set connConnection = New ADODB.Connection
connConnection.CursorLocation = adUseClient
stropen = "select * from 全部信息情况 where 姓名='" & Text1.Text & "'"
connConnection.Open strConnect
With DataCmd
.ActiveConnection = connConnection
.CommandType = adCmdText
.CommandText = stropen
.Execute
End With
With DataRec
.Source = stropen
.ActiveConnection = connConnection
.CursorLocation = adUseClient
.Open DataCmd
End With
Exit Sub
End Sub
Private Sub Command1_Click()
Call ShowReport
End Sub
Private Sub ShowReport()
Dim ControlX As Object
Dim FieldCount As Integer
FieldCount = 0
With DataReport1
.Hide
Set .DataSource = DataRec
.DataMember = ""
With .Sections("Section1")
For Each ControlX In .Controls
If TypeOf ControlX Is RptTextBox Then
ControlX.DataMember = ""
ControlX.DataField = DataRec.Fields(FieldCount).Name
FieldCount = FieldCount + 1
End If
Next
End With
.Refresh
.Show
End With
End Sub
为什么出现的是一个空白表 而如果把sql语句中'" & Text1.Text & "'"换成人名 如 张三 就能正确的显示出张三的信息 为什么不能动态的设置? 展开
Private Sub Form_Load()
Dim strConnect As String
Dim strProvider As String
Dim strDataSource As String
Dim strDataBaseName As String
Dim pt As String
strProvider = "Provider= Microsoft.Jet.OLEDB.4.0;"
strDataSource = App.Path
strDataBaseName = "\数据库(最新2008-3-13新)zhang82519142.mdb;"
strDataSource = "Data Source=" & strDataSource & _
strDataBaseName
strConnect = strProvider & strDataSource
Set connConnection = New ADODB.Connection
connConnection.CursorLocation = adUseClient
stropen = "select * from 全部信息情况 where 姓名='" & Text1.Text & "'"
connConnection.Open strConnect
With DataCmd
.ActiveConnection = connConnection
.CommandType = adCmdText
.CommandText = stropen
.Execute
End With
With DataRec
.Source = stropen
.ActiveConnection = connConnection
.CursorLocation = adUseClient
.Open DataCmd
End With
Exit Sub
End Sub
Private Sub Command1_Click()
Call ShowReport
End Sub
Private Sub ShowReport()
Dim ControlX As Object
Dim FieldCount As Integer
FieldCount = 0
With DataReport1
.Hide
Set .DataSource = DataRec
.DataMember = ""
With .Sections("Section1")
For Each ControlX In .Controls
If TypeOf ControlX Is RptTextBox Then
ControlX.DataMember = ""
ControlX.DataField = DataRec.Fields(FieldCount).Name
FieldCount = FieldCount + 1
End If
Next
End With
.Refresh
.Show
End With
End Sub
为什么出现的是一个空白表 而如果把sql语句中'" & Text1.Text & "'"换成人名 如 张三 就能正确的显示出张三的信息 为什么不能动态的设置? 展开
2个回答
展开全部
可以动态设置的:按你的语句,ADO会查找姓名为 & Text1.Text & 的人,而不是姓名=变量TEXT1.TEXT的人。当然是空白了
stropen = "select * from 全部信息情况 where 姓名='" & Text1.Text & "'"
改一下:stropen = "select * from 全部信息情况 where 姓名=" & '"'&Text1.Text&'"'
如果TEXT1.TEXT=李四,这句的执行结果就是:stropen = "select * from 全部信息情况 where 姓名="李四" " 这条语句只是一个VB中的常量字符串和变量字符串连接运算,这条语句应该完成四个字符串的相加,1、"select * from 全部信息情况 where 姓名=" 2、'"'
3、text1.text 4、 '"'
stropen = "select * from 全部信息情况 where 姓名='" & Text1.Text & "'"
改一下:stropen = "select * from 全部信息情况 where 姓名=" & '"'&Text1.Text&'"'
如果TEXT1.TEXT=李四,这句的执行结果就是:stropen = "select * from 全部信息情况 where 姓名="李四" " 这条语句只是一个VB中的常量字符串和变量字符串连接运算,这条语句应该完成四个字符串的相加,1、"select * from 全部信息情况 where 姓名=" 2、'"'
3、text1.text 4、 '"'
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询