|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
楼主,帮忙看下,我按照案例改了链接的access数据库信息等,然后就报错【下标越界】,请问这是什么情况呢??
- '模糊查询的核心代码。可把需要查询的字段连成一个字符串,用Instr函数去匹配。
- '也可用Like语句 + 通配符* + Or语句实现。用Like的好处是在查询框也可用*通配符
- Private Sub TextBox1_Change()
- Dim strSearchText As String
- Dim lstItem As ListItem
- If rst Is Nothing Then Exit Sub
- If blnNewItem Then MsgBox "有新增行未保存,不可以查询!", vbCritical: Exit Sub
- With ListView1
- .ListItems.Clear
- If rst.EOF And rst.BOF Then Exit Sub '空数据库
- rst.movefirst
- Do While Not rst.EOF
- strSearchText = rst(13) & "/" & rst(14) & "/" & rst(15) '按零部件号/图纸号/零部件名称其中之一查找
- If InStr(strSearchText, UCase(TextBox1)) Then
- Set lstItem = .ListItems.Add
- lstItem.Text = rst(0)
- For j = 1 To rst.fields.Count - 1
- lstItem.SubItems(j) = rst(j)
- Next
- End If
- rst.movenext
- Loop
- End With
- End Sub
- '窗体初始化。给Listview赋值。更改Listview和InkEdit的窗口程序
- Private Sub UserForm_Initialize()
- Dim lstItem As ListItem
- Dim sql As String, arrWidth
- Dim i As Long, j As Long
- Set cnn = CreateObject("adodb.connection")
- Set rst = CreateObject("adodb.recordset")
- cnn.CursorLocation = 2 '游标位置=服务器
- cnn.Open "Provider=Microsoft.ACE.Oledb.12.0;Data Source=" & ThisWorkbook.Path & "\BOM数据库.mdb"
- sql = "select * from DBOM"
- rst.Open sql, cnn, 1, 3, 1
- With ListView1
- .Gridlines = True
- .FullRowSelect = True
- .LabelEdit = lvwManual
- .View = lvwReport
- .SmallIcons = ImageList1
- .Font.Size = 12 '字号:小四
- arrWidth = Array(80, 30, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15, 1, 80, 80, 80, 80, 100, 20, 10, 10, 10, 10, 20, 80, 20, 40, 40, 10, 10, 10, 10, 50, 80, 50)
- For i = 0 To rst.fields.Count - 1
- .ColumnHeaders.Add , , rst(i).Name, arrWidth(i)
- Next
- Do While Not rst.EOF
- Set lstItem = .ListItems.Add
- lstItem.Text = rst(0)
- For j = 1 To rst.fields.Count - 1
- lstItem.SubItems(j) = IIf(IsNull(rst(j)), "", rst(j))
- Next
- rst.movenext
- Loop
- End With
-
- 'InkEdit1.MultiLine = False'提示只读属性。只可在设计窗体时设置。
- 'InkEdit1.BackColor = &HFFFFC0
- 'InkEdit1.BorderStyle = rtfNoBorder
- InkEdit1.Font.Size = ListView1.Font.Size
- InkEdit1.Width = 0
- InkEdit1.ZOrder 0 '把InkEdit1移到最上一层,避免被Listview遮住
-
- sngPixelPerPoint = Pixel2PointX(1) '只计算一次,节约资源
- blnFlag = True ''指示InkEdit1_Exit事件是否保存修改。按下Escape键时设为False
- blnNewItem = False '新增一行标识符。如果新增行未保存或未删除,该标识为TRUE
- TextBox1.SetFocus
-
- strAllowEditCol = "04/05/06/07/08/09/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36" '指定Listview的4~36列可编辑,不指定该参数时则所有列可编辑
- strRequiredCol = "16/17/18" '设定16-18列必填,不指定该参数时则所有列可不填
-
- LvmPreWndProc = GetWindowLong(ListView1.hWnd, GWL_WNDPROC)
- InkPreWndProc = GetWindowLong(InkEdit1.hWnd, GWL_WNDPROC)
- SetWindowLong ListView1.hWnd, GWL_WNDPROC, AddressOf WndProc
- SetWindowLong InkEdit1.hWnd, GWL_WNDPROC, AddressOf WndProc
- End Sub
- '关闭窗体时,还原Listview和InkEdit控件的窗口程序
- Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
- SetWindowLong ListView1.hWnd, GWL_WNDPROC, LvmPreWndProc
- SetWindowLong InkEdit1.hWnd, GWL_WNDPROC, InkPreWndProc
- End Sub
- Private Sub ListView1_Click() '使用单击,在编辑大量数据时更优
- ShowInkEdit
- End Sub
- Private Sub ListView1_DblClick() '双击事件
- ShowInkEdit
- End Sub
复制代码 |
|