连接数据库后,组合框列表不会显示 [英] Combo box list does not show up after connecting database

查看:78
本文介绍了连接数据库后,组合框列表不会显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Public Sub UserForm_Initialize()

On Error GoTo UserForm_Initialize_Err

Dim cnn As New ADODB.Connection

Dim rst as New ADODB.Recordset

cnn.Open(Provider = Microsoft.Jet.OLEDB.4.0;&Data Source =& Application.StartupPath&\Attendance.mdb)

rst.Open(SELECT DISTINCT Department FROM Attendance;)

cnn.adOpenStatic()



rst.MoveFirst( )



使用Me.ComboBox1

.Items.Clear()







.Items.Add(rst![部门])

rst.MoveNext()

循环直到rst.EOF

结束

UserForm_Initialize_Exit:

On Error Resume Next

rst.Close()

cnn.Close()

rst = Nothing

cn n =没有什么

退出Sub

UserForm_Initialize_Err:

MsgBox(Err.Number& vbCrLf& Err.Description,vbCritical,错误!)

恢复UserForm_Initialize_Exit



我尝试过: < br $>


.Items.Add(rst![Departments])

改变了这个.Items.Add(rst.fields())

Public Sub UserForm_Initialize()
On Error GoTo UserForm_Initialize_Err
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.Open("Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & Application.StartupPath & "\Attendance.mdb")
rst.Open("SELECT DISTINCT Department FROM Attendance;")
cnn.adOpenStatic()

rst.MoveFirst()

With Me.ComboBox1
.Items.Clear()

Do

.Items.Add(rst![Departments])
rst.MoveNext()
Loop Until rst.EOF
End With
UserForm_Initialize_Exit:
On Error Resume Next
rst.Close()
cnn.Close()
rst = Nothing
cnn = Nothing
Exit Sub
UserForm_Initialize_Err:
MsgBox(Err.Number & vbCrLf & Err.Description, vbCritical, "Error!")
Resume UserForm_Initialize_Exit

What I have tried:

.Items.Add(rst![Departments])
changed this .Items.Add(rst.fields())

推荐答案

您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你接近一个bug。



使用调试器确保你的代码符合预期,确保数据符合你的想法并缩小研究范围。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

With the debugger ensure that your code do what you expect, ensure that the data is what you think and narrow the research.


你可以改变这个:



rst.Open(SELECT DISTINCT Department FROM Attendance;)

cnn.adOpenStatic()



到此:

cnn.adOpenStatic()

rst。打开(SELECT DISTINCT Department FROM Attendance;)
can you change this :

rst.Open("SELECT DISTINCT Department FROM Attendance;")
cnn.adOpenStatic()

to this:
cnn.adOpenStatic()
rst.Open("SELECT DISTINCT Department FROM Attendance;")


这篇关于连接数据库后,组合框列表不会显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆