|
本人水平较差,在编制一个查询时,本想在一个文本框中输入学校名称作为查询变量。但是老是不成功,请各位行家指点迷津。万谢!!!
Private Sub CommandButton1_Click()
'初始设置===
On Error GoTo gg
Dim CNN As New ADODB.Connection
Dim RST As New ADODB.Recordset
Dim PATH As String
Dim CNNSTRING As String
Dim SQL As String
Dim I As Integer
PATH = ThisWorkbook.PATH & "\历年数据.xls"
CNNSTRING = "PROVIDER=MICROSOFT.JET.OLEDB.4.0;" & _
"EXTENDED PROPERTIES='EXCEL 8.0;HDR=YES;IMEX=1';" & _
"DATA SOURCE=" & PATH
Dim leinie As String, xxmc As String, yx As String
Dim wkjh As Integer, wkbl As Integer, wktu As Integer, wkzg As Integer, wkzd As Integer, wkyb As Integer, wkck As Integer
Dim lkjh As Integer, lkbl As Integer, lktu As Integer, lkzg As Integer, lkzd As Integer, lkyb As Integer, lkck As Integer
'If leibie = "文科" Then
CNN.Open CNNSTRING
xxmc = TextBox1.Text 这是文本框的值
'SQL = "select * from [2013年] where 学校 like xxmc"
SQL = "select * from [2013年] where 学校 like" & "&xxmc"
RST.Open SQL, CNN
'Worksheets("sheet1").Select
Range("A1").CurrentRegion.Clear
For I = I To RST.Fields.Count - 1
Range("A1").Offset(0, I) = RST.Fields(I).Name
Next
Range("A1").Offset(1, 0).CopyFromRecordset RST
RST.Close
CNN.Close
Set RST = Nothing
Set CNN = Nothing
Exit Sub
gg: MsgBox ("您输入的SQL语句有错误,请修正!")
End Sub
{:soso_e183:}{:soso_e181:}{:soso_e163:} |
|