Placing radio button control on form on runtime

I want to place radio button control in a frame on runtime on the base of rows return by a query.

Means if query return 5 rows then place 5 radio button and so on.

please guide with code.
Who is Participating?
jasonhollandConnect With a Mentor Commented:
Here ya go, this should make it completely explanatory.

My code here is a bit tighter, and more dynamic. Just create a new VB Standard EXE,
copy all of the code below into form1, and follow the instructions in the comments.

If you need more help, post another question.

'// Create a standard VB exe
'// Leave Form1 as Form1
'// Create a command button, leave the name as command1, change the caption to "Records Returned"
'// Create another command button, leave the name as command2, change the caption to "Which one is selected"
'// Create a frame, leave the name as Frame1
'// Create 1 option (radio button) on the frame. Make sure it is on the frame.
'// Leave the name as Option 1, change the caption to "Option1(0), change the index value to 0
'// this will initialize it as an array, set Option1's height to 255, or whatever you like. I used 255.

Private Sub Command1_Click()
    '// This simulates the number of records returned from your query
    Dim i As Integer
    i = InputBox("How many records were returned?", "Return Record Count")
    '// When you get the number of records returned, call the sub like this
    Call DBQueryReturn(i)
End Sub

Sub DBQueryReturn(recordsReturned As Integer)

'// call this with the number of records returned from your query
Dim c, o As Integer
'// Start of the array
o = 1
'// This is the current count in the array
c = Form1.Option1.Count
'// As long as the new return amount is not 0, start adding option buttons
    If recordsReturned > 0 Then
        '// keep adding the option buttons until we are done
        Do While o <= recordsReturned
            '// create a new option button object
            Load Option1(c)
            '// change the forms height, adding the height of the new option control
            Form1.Height = Form1.Height + Option1(c).Height
            '// change the frames height, adding the height of the new option control
            Form1.Frame1.Height = Form1.Frame1.Height + Option1(c).Height
            '// put the new option control under the last one on the form, the 10 is for a small gap between them
            Form1.Option1(c).Top = Form1.Option1(c - 1).Top + Form1.Option1(c).Height + 10
            '// Set the options to visible
            Form1.Option1(c).Visible = True
            '// Change the caption
            Form1.Option1(c).Caption = "Option1(" & c & ")"
            c = c + 1
            o = o + 1
    End If

End Sub

Private Sub Command2_Click()
    '// This just shows you how to figure out which one was selected
    Dim vControl As Variant
    Dim x As Integer
    x = 0
    '// loop through all of the option controls on form1
    For Each vControl In Form1.Option1
        '// if the current one is selected
        If Form1.Option1(x).Value = True Then
            '// show a message box
            MsgBox "Option1(" & x & ") was selected"
        End If
        x = x + 1
End Sub

Private Sub Form_Resize()
    '// Move the command buttons down the form so that it is still visible
    Form1.Command1.Top = Form1.ScaleHeight - Form1.Command1.Height - 25
    Form1.Command2.Top = Form1.ScaleHeight - Form1.Command2.Height - 25
End Sub

Good luck
Try adding a radio button to your frame (Option1) and change the index property to 0. Then try this to add more option buttons:

Private Sub Command1_Click()
Load Option1(1)
Option1(1).Top = Option1(1 - 1).Top + Option1(1 - 1).Height
Option1(1).Visible = True

Load Option1(2)
Option1(2).Top = Option1(2 - 1).Top + Option1(2 - 1).Height
Option1(2).Visible = True

Load Option1(3)
Option1(3).Top = Option1(3 - 1).Top + Option1(3 - 1).Height
Option1(3).Visible = True

End Sub
pak_slmAuthor Commented:
Also need to resize frame and form height with radio button.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

You could use a select case statement in the option buttons click event.For example,this will change the form and frame height when you select  Option1(3)

Private Sub Option1_Click(Index As Integer)
Select Case Index
    Case 1
    Case 2
    Case 3
        If Option1(3).Value = True Then
            Form1.Height = 6000
            Frame1.Height = 3000
        End If
End Select
End Sub
pak_slmAuthor Commented:

I try code it is ok
I also check within recordset loop it is ok

How i know which is clicked.

You can tell by the index passed to the option buttons click event.For example:

Private Sub Option1_Click(Index As Integer)
Select Case Index
    Case 1
        MsgBox "Option1(1) has been clicked"
    Case 2
        MsgBox "Option1(2) has been clicked"
    Case 3
        MsgBox "Option1(3) has been clicked"
        Form1.Height = 6000
        Frame1.Height = 3000
End Select
End Sub
do not accept this as an answer

If you don't know how many

just do

Private Sub Option1_Click(Index As Integer)
MsgBox "Option1(" & index & ") has been clicked"

End Sub
pak_slmAuthor Commented:

Very nice, One thing more need to be clear

i have query like

select code, desc from table1

i want to set option button caption as desc and when i click one of those then code should be return.

please guide with code accordingly.

Could you please post a couple of your code, desc pairs from your database. It really depends on how much data is being held in the code
record as to how I would deal with it. Either way, it can be done without a prob...

Jason Holland
pak_slmAuthor Commented:

i have used procedure Private Sub Command2_Click() and set filter on Rs on the base of selected option button caption and get Code.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.