1.判断日期是否为空
2.SQL里日期值需用#引用
3. and连接符前需有一个空格
If Len(ComboBox5.Text) = 0 Or Len(ComboBox6.Text) = 0 Then MsgBox "起始或结束日期不能为空!" Exit Sub End If Set CNN = CreateObject("ADODB.Connection") CNN.Open "provider=microsoft.jet.oledb.4.0;extended properties=excel 8.0;data source=" & ThisWorkbook.FullName Sql = " select 柜号,姓名,编号,类别,调出,备注,链接档案目录,日期 from [数据$] where 日期>=#" & ComboBox5.Text & "# and 日期<=#" & ComboBox6.Text & "#" If ComboBox1.Text <> "" Then Sql = Sql & " and 柜号 like '%" & ComboBox1.Text & "%'" If ComboBox2.Text <> "" Then Sql = Sql & " and 姓名 like '%" & ComboBox2.Text & "%'" If ComboBox3.Text <> "" Then Sql = Sql & " and 编号 like '%" & ComboBox3.Text & "%'" If ComboBox4.Text <> "" Then Sql = Sql & " and 类别 like '%" & ComboBox4.Text & "%'" Set rst = CreateObject("adodb.recordset") rst.Open Sql, CNN, adOpenKeyset, adLockPessimistic Sheet2.[a2].CopyFromRecordset rst Set rst = Nothing Set CNN = Nothing Unload Me |