Slashback115
Registered User.
- Local time
- Today, 08:36
- Joined
- Sep 27, 2016
- Messages
- 18
I am working on a project (for fun) and am almost complete but stumped on this last little part. Any help would be greatly appreciated.
This is for Access 2013
I have a basic Excel table with names. Artname, Dwarven, Elvish, Human.
Button (artbutton)
ComboBox (selectname)
Text Box (arttext)
Table (Gen)
Query (Genq)
I need to have the combo box select 1 of the many columns. Once selected the button (on_click) will randomize all the values in that column and display one in the text field.
I have it working to the point of selecting the first field Artname and randomizing it. But no matter what I try, I cannot get the button to recognize the combobox selection.
Here is the code I currently have. Thanks again!
(This works for the 1 random column.)
----------------------------------------------------------------------------------------------------------
Private Sub artbutton_Click()
Dim MinID As Integer
Dim MaxID As Integer
Dim RandomVal As Integer
Dim arttext As String
MinID = DMin("ID", "Gen")
MaxID = DMax("ID", "Gen")
Randomize
RandomVal = Int((MaxID * Rnd) + MinID)
arttext = getRandomStuff(RandomVal)
Me.arttext.Value = arttext
End Sub
Public Function getRandomStuff(lookupVal As Integer) As String
Dim rst As DAO.Recordset
Dim sqlStr As String
sqlStr = "Select Gen.Artname FROM Gen Where Gen.ID = " & lookupVal & ""
Set rst = CurrentDb.OpenRecordset(sqlStr)
getRandomStuff = rst!Artname
rst.Close
Set rst = Nothing
End Function
----------------------------------------------------------------------------------------------------------
This was my attempt getting it to work with multiple columns.
----------------------------------------------------------------------------------------------------------
Private Sub artbutton_Combo(Rannum)
Dim MinID As Integer
Dim MaxID As Integer
Dim RandomVal As Integer
Dim arttext As String
MinID = DMin("ID", "Gen")
MaxID = DMax("ID", "Gen")
If Ranname = "Artname" Then MinID = 2 & MaxID = 2047
If Ranname = "Dwarven" Then MinID = 2 & MaxID = 1231
If Ranname = "Drow M" Then MinID = 2 & MaxID = 1907
If Ranname = "High Elf" Then MinID = 2 & MaxID = 838
If Ranname = "Eastern" Then MinID = 2 & MaxID = 1225
If Ranname = "Orcish" Then MinID = 2 & MaxID = 1747
If Ranname = "Gnomish" Then MinID = 2 & MaxID = 933
If Ranname = "Roman" Then MinID = 2 & MaxID = 847
If Ranname = "Common Male" Then MinID = 2 & MaxID = 318
If Ranname = "Common Female" Then MinID = 2 & MaxID = 345
If Ranname = "Location" Then MinID = 2 & MaxID = 959
Randomize
RandomVal = Int((MaxID * Rnd) + MinID)
arttext = getRandomStuff(RandomVal)
Me.arttext.Value = arttext
End Sub
Public Function getRandomStuff(lookupVal As Integer) As String
Dim rst As DAO.Recordset
Dim sqlStr As String
sqlStr = "Select Gen.Artname FROM Gen Where Gen.ID = " & lookupVal & ""
Set rst = CurrentDb.OpenRecordset(sqlStr)
getRandomStuff = rst!Artname
rst.Close
Set rst = Nothing
End Function
Private Sub selectname_OnChange()
Dim Ranname As String
Ranname = selectname.Selected
artbutton_Combo Ranname
End Sub
----------------------------------------------------------------------------------------------------------
This is for Access 2013
I have a basic Excel table with names. Artname, Dwarven, Elvish, Human.
Button (artbutton)
ComboBox (selectname)
Text Box (arttext)
Table (Gen)
Query (Genq)
I need to have the combo box select 1 of the many columns. Once selected the button (on_click) will randomize all the values in that column and display one in the text field.
I have it working to the point of selecting the first field Artname and randomizing it. But no matter what I try, I cannot get the button to recognize the combobox selection.
Here is the code I currently have. Thanks again!
(This works for the 1 random column.)
----------------------------------------------------------------------------------------------------------
Private Sub artbutton_Click()
Dim MinID As Integer
Dim MaxID As Integer
Dim RandomVal As Integer
Dim arttext As String
MinID = DMin("ID", "Gen")
MaxID = DMax("ID", "Gen")
Randomize
RandomVal = Int((MaxID * Rnd) + MinID)
arttext = getRandomStuff(RandomVal)
Me.arttext.Value = arttext
End Sub
Public Function getRandomStuff(lookupVal As Integer) As String
Dim rst As DAO.Recordset
Dim sqlStr As String
sqlStr = "Select Gen.Artname FROM Gen Where Gen.ID = " & lookupVal & ""
Set rst = CurrentDb.OpenRecordset(sqlStr)
getRandomStuff = rst!Artname
rst.Close
Set rst = Nothing
End Function
----------------------------------------------------------------------------------------------------------
This was my attempt getting it to work with multiple columns.
----------------------------------------------------------------------------------------------------------
Private Sub artbutton_Combo(Rannum)
Dim MinID As Integer
Dim MaxID As Integer
Dim RandomVal As Integer
Dim arttext As String
MinID = DMin("ID", "Gen")
MaxID = DMax("ID", "Gen")
If Ranname = "Artname" Then MinID = 2 & MaxID = 2047
If Ranname = "Dwarven" Then MinID = 2 & MaxID = 1231
If Ranname = "Drow M" Then MinID = 2 & MaxID = 1907
If Ranname = "High Elf" Then MinID = 2 & MaxID = 838
If Ranname = "Eastern" Then MinID = 2 & MaxID = 1225
If Ranname = "Orcish" Then MinID = 2 & MaxID = 1747
If Ranname = "Gnomish" Then MinID = 2 & MaxID = 933
If Ranname = "Roman" Then MinID = 2 & MaxID = 847
If Ranname = "Common Male" Then MinID = 2 & MaxID = 318
If Ranname = "Common Female" Then MinID = 2 & MaxID = 345
If Ranname = "Location" Then MinID = 2 & MaxID = 959
Randomize
RandomVal = Int((MaxID * Rnd) + MinID)
arttext = getRandomStuff(RandomVal)
Me.arttext.Value = arttext
End Sub
Public Function getRandomStuff(lookupVal As Integer) As String
Dim rst As DAO.Recordset
Dim sqlStr As String
sqlStr = "Select Gen.Artname FROM Gen Where Gen.ID = " & lookupVal & ""
Set rst = CurrentDb.OpenRecordset(sqlStr)
getRandomStuff = rst!Artname
rst.Close
Set rst = Nothing
End Function
Private Sub selectname_OnChange()
Dim Ranname As String
Ranname = selectname.Selected
artbutton_Combo Ranname
End Sub
----------------------------------------------------------------------------------------------------------