VB访问数据库,进行查询,结果用datagrid控件显示在VB界面.
设表A中有a,b,c d,e,f,g这七个字段,
如果固定查询其中a,b,c三个字段,
可以用 StrSql="select a,b,c from A"代替下面程序的相关部分即可.
现在我想在窗口选择需要查找的字段,然后进行查询,该怎么做?
我用 temp=text1.text & "," & text2.text & "," & text3.text
然后 StrSql="select temp from A"
程序提示:temp没有初始值Private Sub Form_Load()
Dim cn As New ADODB.Connection
Dim Cmd As New ADODB.Command
Dim rs As New ADODB.Recordset
Dim strConnect As String
Dim StrSql As String
Dim temp As String
StrSql = "select * from A" Set cn = New ADODB.Connection
strConnect = "Provider=; Data Source=
cn.ConnectionString = strConnect
cn.Open
With Cmd
.ActiveConnection = cn
.CommandType = adCmdText
.CommandText = StrSql
....... Set DataGrid1.DataSource = rs
End Sub
设表A中有a,b,c d,e,f,g这七个字段,
如果固定查询其中a,b,c三个字段,
可以用 StrSql="select a,b,c from A"代替下面程序的相关部分即可.
现在我想在窗口选择需要查找的字段,然后进行查询,该怎么做?
我用 temp=text1.text & "," & text2.text & "," & text3.text
然后 StrSql="select temp from A"
程序提示:temp没有初始值Private Sub Form_Load()
Dim cn As New ADODB.Connection
Dim Cmd As New ADODB.Command
Dim rs As New ADODB.Recordset
Dim strConnect As String
Dim StrSql As String
Dim temp As String
StrSql = "select * from A" Set cn = New ADODB.Connection
strConnect = "Provider=; Data Source=
cn.ConnectionString = strConnect
cn.Open
With Cmd
.ActiveConnection = cn
.CommandType = adCmdText
.CommandText = StrSql
....... Set DataGrid1.DataSource = rs
End Sub
解决方案 »
- VB如何打包crystal report的chart dll?
- 如何用VB代码进行3721的卸载!!!
- 执行语句的问题
- 请问VB+SQL基于局域网编程,如何把连接的信息存到ini文件或者写进注册表中以达到连接的信息可以随时改变?
- 怎麼獲取.exe文件的版本號?
- 请问VB中挂库的中的SQL语句中可以插入VB函数吗?
- 在VB中如何做出windows copy文件时的出现的窗口。
- VB的速度问题
- 山西的兄弟请到这里签到!!
- 烦请各路豪杰帮“弱学”一把,毕业设计等待解决?
- dhtmledit控件如何插入表格(急)
- 从数据库中取出三个表的数据,要把这些数据存入excel 同时,数据之间要有一些相关的计算。如何实现。在线等...
在设置adodc1的属性的时候要小心。
字记录源的命令类型中选第一个。
表或存储过程名称会变成灰色的。
在命令文本输入
select * from 表名接着在recordsource属性select * from 表名这些设置都是在属性框中进行的。设置好后
代码就像我帖子那样写就可以了
Object = "{67397AA1-7FB1-11D0-B148-00A0C922E820}#6.0#0"; "MSADODC.OCX"
Object = "{CDE57A40-8B86-11D0-B3C6-00A0C90AEA82}#1.0#0"; "MSDATGRD.OCX"
Begin VB.Form Form4
Caption = "Form4"
ClientHeight = 4095
ClientLeft = 60
ClientTop = 345
ClientWidth = 7950
LinkTopic = "Form4"
ScaleHeight = 4095
ScaleWidth = 7950
StartUpPosition = 3 '窗口缺省
Begin VB.TextBox Text2
Height = 375
Left = 3600
TabIndex = 5
Top = 3360
Width = 975
End
Begin VB.TextBox Text1
Height = 375
Left = 1320
TabIndex = 2
Top = 3360
Width = 975
End
Begin VB.CommandButton Command1
Caption = "查询"
Height = 375
Left = 5160
TabIndex = 1
Top = 3360
Width = 975
End
Begin MSAdodcLib.Adodc Adodc1
Height = 495
Left = 1440
Top = 0
Visible = 0 'False
Width = 5415
_ExtentX = 9551
_ExtentY = 873
ConnectMode = 0
CursorLocation = 3
IsolationLevel = -1
ConnectionTimeout= 15
CommandTimeout = 30
CursorType = 3
LockType = 3
CommandType = 8
CursorOptions = 0
CacheSize = 50
MaxRecords = 0
BOFAction = 0
EOFAction = 0
ConnectStringType= 1
Appearance = 1
BackColor = -2147483643
ForeColor = -2147483640
Orientation = 0
Enabled = -1
Connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\vbkeshe\财务管理.mdb;Mode=ReadWrite|Share Deny None;Persist Security Info=False"
OLEDBString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\vbkeshe\财务管理.mdb;Mode=ReadWrite|Share Deny None;Persist Security Info=False"
OLEDBFile = ""
DataSourceName = ""
OtherAttributes = ""
UserName = ""
Password = ""
RecordSource = "select * from caiwu"
Caption = "Adodc1"
BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851}
Name = "宋体"
Size = 9
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
_Version = 393216
End
Begin MSDataGridLib.DataGrid DataGrid1
Bindings = "grid.frx":0000
Height = 2655
Left = 360
TabIndex = 0
Top = 480
Width = 7215
_ExtentX = 12726
_ExtentY = 4683
_Version = 393216
HeadLines = 1
RowHeight = 15
BeginProperty HeadFont {0BE35203-8F91-11CE-9DE3-00AA004BB851}
Name = "宋体"
Size = 9
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851}
Name = "宋体"
Size = 9
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ColumnCount = 2
BeginProperty Column00
DataField = ""
Caption = ""
BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED}
Type = 0
Format = ""
HaveTrueFalseNull= 0
FirstDayOfWeek = 0
FirstWeekOfYear = 0
LCID = 2052
SubFormatType = 0
EndProperty
EndProperty
BeginProperty Column01
DataField = ""
Caption = ""
BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED}
Type = 0
Format = ""
HaveTrueFalseNull= 0
FirstDayOfWeek = 0
FirstWeekOfYear = 0
LCID = 2052
SubFormatType = 0
EndProperty
EndProperty
SplitCount = 1
BeginProperty Split0
BeginProperty Column00
EndProperty
BeginProperty Column01
EndProperty
EndProperty
End
Begin VB.Label Label2
AutoSize = -1 'True
Caption = "姓名"
Height = 180
Left = 3000
TabIndex = 4
Top = 3480
Width = 360
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "时间"
Height = 180
Left = 720
TabIndex = 3
Top = 3480
Width = 360
End
End
Attribute VB_Name = "Form4"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Sub Command1_Click()
If Text2.Text <> "" Then
Adodc1.RecordSource = "Select * From caiwu Where 姓名 ='" & Text2.Text & "'"
Adodc1.Refresh
End If
If Text1.Text <> "" Then
Adodc1.RecordSource = "Select * From caiwu Where 时间 ='" & Text2.Text & "'"
Adodc1.Refresh
End If
If Text1.Text <> "" And Text2.Text Then
Adodc1.RecordSource = "Select * From caiwu Where 姓名 ='" & Text2.Text & "'" & "时间='" + Text1.Text + "'"
Adodc1.Refresh
End If
End Sub
保存为grid.frm试试