Placing radio button control on form on runtime

Posted on 2004-03-26
Last Modified: 2013-12-26
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.
Question by:pak_slm
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
LVL 28

Expert Comment

ID: 10686630
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

Author Comment

ID: 10686713
Also need to resize frame and form height with radio button.
LVL 28

Expert Comment

ID: 10686879
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 10687182

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

How i know which is clicked.

LVL 28

Expert Comment

ID: 10687347
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
LVL 26

Expert Comment

ID: 10687648
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

Accepted Solution

jasonholland earned 125 total points
ID: 10688845
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

Author Comment

ID: 10694138

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.


Expert Comment

ID: 10695007
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

Author Comment

ID: 10701841

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


Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Using "ScreenUpdating" 6 84
VBA: copy range dynamically based on config sheet v2 3 55
Problem to line 23 74
"Failed to convert parameter value from a String to a Decimal." 4 32
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question