LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 812
Default Populate Userform Listbox with Access values

works fine, i now need to be able to update the same recordset feilds
on a seperate userform, do you have any exampleas of this please??


The following code uses the same UserForm. With a seperate UserForm,
you would still need a control such as a ListBox to see what is in
Table1. I added 2 controls (CommandButton1 and TextBox1) that allow
changing the Age field in Table1. Note that most of the code supplied
earlier has been moved.

Hth,
Merjet

Private db As Database

Private Sub CommandButton1_Click()
Dim rs As Recordset
Dim bFound As Boolean

If TextBox1 < "" And ListBox1.ListIndex -1 Then
Set rs = db.OpenRecordset("Table1")
rs.MoveFirst
Do
With ListBox1
' if Table1 has primary key, need only
' check for match with primary field
If rs("Name") = .List(.ListIndex, 0) And _
rs("Age") = CInt(.List(.ListIndex, 1)) And _
rs("SDate") = CDate(.List(.ListIndex, 2)) And _
rs("Pay") = CInt(.List(.ListIndex, 3)) Then
rs.Edit
rs("Age") = CInt(TextBox1)
rs.Update
bFound = True
End If
End With
rs.MoveNext
Loop Until rs.EOF Or bFound = True
End If
Set rs = Nothing
FillListBox1
End Sub

Private Sub UserForm_Activate()
Set db = OpenDatabase("C:\temp\db1.mdb")
FillListBox1
End Sub

Private Sub UserForm_Deactivate()
db.Close
Set db = Nothing
End Sub

Private Sub FillListBox1()
Dim rs As Recordset
Dim Sql As String
Sql = "SELECT * FROM Table1 WHERE " _
& "Name = " & Chr(34) & "Smith" & Chr(34)
Set rs = db.OpenRecordset(Sql)
ListBox1.Clear
With rs
If Not .BOF Then .MoveFirst
While Not .EOF
With ListBox1
.AddItem rs("Name")
.List(.ListCount - 1, 1) = rs("Age")
.List(.ListCount - 1, 2) = rs("SDate")
.List(.ListCount - 1, 3) = rs("Pay")
End With
.MoveNext
Wend
End With
Me.Repaint
Set rs = Nothing
End Sub




 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
userform listbox cannot get listbox.value to transfer back to main sub [email protected] Excel Programming 1 May 17th 06 09:44 PM
Populate userform listbox from access db Nancy Moon Excel Programming 0 March 1st 06 09:20 PM
Initialize Userform, Populate Listbox, Dynamic RowSource? RShow Excel Programming 1 September 21st 05 07:55 PM
Populate a ListBox JB in Kansas Excel Programming 6 September 19th 05 03:01 PM
populate listbox JSnader Excel Programming 2 December 6th 03 02:43 PM


All times are GMT +1. The time now is 12:12 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"