回复 第13楼 不说也罢 的帖子
出错了。我按照你说的那样子读取数据库,结果提示“对象已经加载”。
这是程序,麻烦你看一下:
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0
MsgBox "你按下了你在窗体设计时画的按钮"
Case Else
MsgBox "你按下了程序添加的按钮" & Index
End Select
End Sub
Private Sub Form_Load()
Dim i As Integer
Dim t As Integer
Dim n As Integer
'''''n是数据库中空闲的房间数
i = 5 '意思是再创建5个按钮,你从数据库里读吧
For t = 1 To i
sql = "select 房间号 from 房间信息 where 能否入住='是'"
'''''读取数据库中空闲的房间号
Set rst = getRS(sql)
rst.MoveFirst
For n = 1 To rst.RecordCount
Load Command1(t) '加载t个按钮,实际上窗体上是t+1个按钮了,你画窗体时画了一个
Command1(t).Visible = True '没有这一句,加载的按钮在窗体上看不见
Command1(t).Caption = rst.Fields(0)
''''''把房间号赋值给按钮名
'下面一句是调整加载后各个按钮的位置,Joforn回答过了,我不啰嗦了
Command1(t).Move Command1(t - 1).Left, Command1(t - 1).Top + Command1(t - 1).Height
rst.MoveNext
Next n
Next t
End Sub
'附:getrs是我创建的过程
'''''''Public Function getRS(ByVal sql As String) As ADODB.Recordset
'创建名为getRS的过程
'''''''Dim con As ADODB.Connection
'''''''Dim rs As ADODB.Recordset
'''''''Dim strConnection As String
'''''''Dim strArray() As String
'创建数组
'''''''Set con = New ADODB.Connection
'''''''Set rs = New ADODB.Recordset
'''''''On Error GoTo getRS_Error
'''''''strConnection = "Provider=Microsoft.jet.oledb.4.0;Data Source=" & App.Path & "\database.mdb"
'''''''
strArray = Split(sql)
'''''''
con.Open strConnection
'''''''
If InStr(UCase$(strArray(0)), "select") = 0 Then
'''''''
rs.Open Trim$(sql), con, adOpenStatic, adLockOptimistic
'''''''
Set getRS = rs
'''''''
Else
'''''''
con.Execute sql
'''''''
End If
[[it] 本帖最后由 xiaomengyzy 于 2008-12-5 20:05 编辑 [/it]]