Okay, I'm new to VB, obviously so I apologize for being so green.

Here is what I'm trying to do, I want to create two boxes that I can generate seperately names from a database randomly. I've been plugging at it for a while and can't quite get it.

I also, in time want to be able to have a button that will add a name to the database from the text box...not even sure if thats possible, but I hope so.

Any help would be cool.

Thanks

The database is access right?

To be honest I don't have the database set up yet.

I guess what I'm asking is what commands do I use to pick a random name from the database. Maybe I'm putting the cart before the horse, but with our without the database I'm still not sure what command to use.

I'm not sure about what commands to use for much of anything actually, I'm very green.

Cool I think this may help.

Thanks!!

Might this code serves your purpose

'used stuffs :- textbox(txtname), two buttons(cmdadd,cmdselectname),list box(list1), database(info), table(info), field("name")

Option Explicit
Dim db As Database, rs As Recordset

'adding a new name to the database based on value supplied into the textbox
Private Sub cmdadd_Click()
If Trim(txtname.Text) <> "" Then
rs.AddNew
If rs.EditMode = dbEditAdd Then
rs("name") = Trim(txtname.Text)
rs.Update
MsgBox "Name added to the database."
txtname.Text = ""
txtname.SetFocus
End If
Else
MsgBox "Plz input some text before trying to add it to the database."
txtname.SetFocus
Exit Sub
End If
End Sub

'extracting all names from the database into a listbox and selecting a random name from it
Private Sub cmdselectname_Click()
Dim rname As Integer

Set rs = db.OpenRecordset("info")
If rs.RecordCount > 0 Then
rs.MoveFirst
List1.Clear
Do Until rs.EOF()
List1.AddItem rs("name")
rs.MoveNext
Loop
Randomize
rname = Int(Rnd() * List1.ListCount)
List1.ListIndex = rname
MsgBox "The following name has been randomly selected from the database :-" & vbCrLf & vbCrLf & "Name : " & List1.Text
Else
MsgBox "No names found in the database." & vbCrLf & "Plz add some names first."
End If
End Sub

Private Sub Form_Load()
Set db = OpenDatabase(App.Path & "\info.mdb")
Set rs = db.OpenRecordset("info", dbOpenTable)
If rs.RecordCount > 0 Then rs.MoveFirst
List1.Visible = False
End Sub

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.