Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Message Button in Form

Status
Not open for further replies.

iuianj07

Programmer
Sep 25, 2009
293
US
Hello all,

I am planning to create a button within form1, that whenever you click on it, form2 will show up that is bound to table1. The form2 only has a label ( or maybe a textbox?) that would contain a random message (record) that is in table1.

Let's just think about it as a "Quote of the Day" button, that in table1 will we store all of the quotes that we put in there. and whenever we click the button within form1, form2 will always show a random message everytime we click that button.

I need help with the VBA on how to program that button to do the task above.
Any help will be highly appreciated.

Thank you
 
Hi there--what have you tried so far?
What is your table structure of "table1"?

Thanks.

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244. Basics at
 
Hello GingerR

The table structure for table1 is just one column named Quote and it has all the quotes that we want to input there.

I have created form1 with the button to open form2. I am in the part now how to figure out to create the VBA code for it to show random records in form2.

Thank you
 
Code:
Public Function getRandomData(fldName As String, domain As String) As Variant
  On Error GoTo errlable
  Dim rs As DAO.Recordset
  Dim lngRecord As Long
  Dim lngCount As Long
  Dim strSql As String
  fldName = "[" & fldName & "]"
  strSql = "Select " & fldName & " from " & domain
  Set rs = CurrentDb.OpenRecordset(strSql, dbOpenDynaset)
  
  If Not (rs.EOF And rs.BOF) Then
    rs.MoveLast
    rs.MoveFirst
    lngCount = rs.RecordCount
    lngRecord = Fix(lngCount * Rnd)
    rs.AbsolutePosition = lngRecord
    getRandomData = rs.Fields(fldName)
  End If
  Exit Function
errlable:
  MsgBox Err.Number & " " & Err.Description
End Function
 
Hello MajP

This is a Public Function right? Is there also a code I should put in the button to show Form2 with the random quote?

THank you
 
Yes put in a standard module

to call the function

Private sub yourClickEvent()
me.someControlName.value = getRandomData("yourFieldName","YourTableOrQueryName")
end
 
Hello,

I apologize, but this might be a silly question:

Private sub yourClickEvent()
me.someControlName.value = getRandomData("yourFieldName","YourTableOrQueryName")
end

what should I declare in "someControlName"?

Thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top