我有7個名爲lblSun,lblMon等文本框和7個名爲cmdSun,cmdMon等的按鈕。我想在查詢中更改這些文本框和按鈕的Text值。我試過Me.Controls("cmd" & daysOfWeek(i)).Text
,但它不起作用。動態更改文本框的值?
的錯誤不是設置爲一個對象的一個實例對象引用。
這裏是我的代碼:
Public Sub loadSchedule()
' days of week
Dim daysOfWeek(0 To 6) As String
Dim i As Integer
Dim var As String
Dim ctrl As Control
' set up the days of the week
daysOfWeek(0) = "Sun"
daysOfWeek(1) = "Mon"
daysOfWeek(2) = "Tue"
daysOfWeek(3) = "Wed"
daysOfWeek(4) = "Thu"
daysOfWeek(5) = "Fri"
daysOfWeek(6) = "Sat"
' connect to the db
Dim con As OleDb.OleDbConnection = New OleDb.OleDbConnection(Form1.conString)
con.Open()
' query stuff
Dim query As String
Dim cmd As New OleDb.OleDbCommand
Dim rs As OleDb.OleDbDataReader
' loop
For i = LBound(daysOfWeek) To UBound(daysOfWeek)
' set query
query = "SELECT * FROM Schedule WHERE Employee=" & employee & " AND ScheduleDay='" & daysOfWeek(i) & "'"
cmd = New OleDb.OleDbCommand(query, con)
rs = cmd.ExecuteReader()
' var
var = "cmd" & daysOfWeek(i)
' any results?
If rs.HasRows = True Then
' get it
rs.Read()
' show it baby
'Controls("lbl" & daysOfWeek(i)).Text = rs.Item("TimeIn") & " - " & rs.Item("TimeOut")
Me.Controls("cmd" & daysOfWeek(i) & "").Text = "Edit"
Else
' show it baby
Controls("lbl" & daysOfWeek(i)).Text = "RDO"
Controls("cmd" & daysOfWeek(i)).Text = "New"
End If
Next
' close db
con.Close()
End Sub
誰能幫助我?我究竟做錯了什麼?
你有你坐的按鈕在面板上? – AbZy 2013-02-17 16:54:30
他們被放置在一個表格(停靠),在一個窗體。 – Peter 2013-02-17 16:58:18