Notes in Hindi

Using Controls and Their Properties

Makhanlal Chaturvedi University / BCA / VBA programming

Using Controls and Their Properties in VBA Forms in Hindi

Using Controls and Their Properties in VBA Forms in Hindi

Overview of Common Controls used in VBA Forms

VBA Forms का उपयोग तब किया जाता है जब हमें Excel या किसी और Microsoft Office एप्लिकेशन में इंटरफ़ेस बनाना होता है जिससे User से Input लिया जा सके। इन Forms में हम कई तरह के Controls का उपयोग करते हैं जो User interaction को आसान और प्रभावी बनाते हैं।

  • Label: यह सिर्फ Text दिखाने के लिए होता है। जैसे - "Enter Name"।
  • TextBox: इसमें User कुछ भी टाइप कर सकता है जैसे नाम, पता आदि।
  • CommandButton: इस बटन को क्लिक करने पर कोई Action perform होता है। जैसे "Submit" या "Reset"।
  • ComboBox: इसमें Drop-down लिस्ट आती है जिसमें से User एक Item चुन सकता है।
  • ListBox: इसमें एक से अधिक आइटम दिखते हैं और User multiple selection भी कर सकता है।
  • CheckBox: इसका उपयोग किसी Option को "Yes" या "No" के रूप में चुनने के लिए होता है।
  • OptionButton: ये radio buttons जैसे होते हैं, जिसमें User एक ही विकल्प चुन सकता है।
  • Frame: यह Controls को समूह में बांधने के लिए होता है।
  • ToggleButton: यह एक On/Off बटन जैसा काम करता है।

How to Set and Modify Properties of Controls in VBA Forms

हर Control की अपनी कुछ Properties होती हैं जिनके जरिए हम उस Control का behavior और appearance बदल सकते हैं। जब हम Form बनाते हैं और Controls को उसमें Add करते हैं, तब हमें उनकी Properties को Modify करना बहुत जरूरी होता है ताकि User Interface बेहतर बन सके।

  • Name: यह Code में उस Control को पहचानने के लिए होता है। जैसे - TextBox1
  • Caption: यह बटन या Label पर दिखने वाला Text होता है।
  • Value: यह CheckBox या OptionButton के लिए True या False हो सकता है।
  • Enabled: यदि यह False है तो वह Control Disabled हो जाएगा।
  • Visible: यह तय करता है कि Control दिखेगा या नहीं।
  • Text: TextBox में लिखा गया Text।
  • BackColor / ForeColor: Background और Font के रंग।
  • Font: Font का Style और Size।

Properties सेट करने के दो तरीके होते हैं:

  • Design Time: जब हम Form को Design कर रहे होते हैं, तब Properties Window से सीधे Property बदल सकते हैं।
  • Run Time: जब Code चल रहा होता है तब हम VBA कोड के माध्यम से Properties बदल सकते हैं।
'Example: CommandButton की Caption बदलना Private Sub UserForm_Initialize() CommandButton1.Caption = "Submit" TextBox1.Text = "" Label1.ForeColor = vbBlue End Sub

Using Events and Control Properties to Enhance Form Functionality

Controls में Events होते हैं जैसे Click, Change, Enter आदि। हम इन Events पर Code लिख सकते हैं जिससे Form की Functionality बढ़ जाती है।

कुछ Common Events:

  • Click: जब कोई Button क्लिक किया जाए।
  • Change: जब TextBox या ComboBox का Text बदले।
  • Enter: जब किसी Control पर Focus आए।
  • Exit: जब Control से बाहर जाया जाए।
'Example: Button पर क्लिक करने पर TextBox का डेटा पढ़ना Private Sub CommandButton1_Click() MsgBox "आपने लिखा है: " & TextBox1.Text End Sub 'Example: ComboBox में चयन होने पर Label में Value दिखाना Private Sub ComboBox1_Change() Label1.Caption = "आपने चुना: " & ComboBox1.Value End Sub

इन Events और Properties का सही उपयोग करके हम Form को Smart, Interactive और User Friendly बना सकते हैं। इससे User को बेहतर अनुभव मिलता है और Data Collection आसान हो जाता है।

Practical Examples of Control Properties in VBA Forms

अब हम कुछ Practical उदाहरण देखेंगे जहां Controls और उनकी Properties को कैसे Real-world Scenarios में Use किया जाता है।

1. Login Form Example

Form में दो TextBox होंगे (Username और Password के लिए), एक CommandButton और एक Label।

Private Sub CommandButton1_Click() If TextBox1.Text = "admin" And TextBox2.Text = "1234" Then Label1.Caption = "Login Successful" Label1.ForeColor = vbGreen Else Label1.Caption = "Invalid Credentials" Label1.ForeColor = vbRed End If End Sub

2. Survey Form Example with CheckBox and OptionButton

  • नाम दर्ज करने के लिए TextBox
  • Gender चुनने के लिए OptionButton
  • Interest चुनने के लिए CheckBoxes
  • Submit करने के लिए Button
Private Sub CommandButton1_Click() Dim gender As String Dim interest As String If OptionButton1.Value = True Then gender = "Male" ElseIf OptionButton2.Value = True Then gender = "Female" Else gender = "Other" End If If CheckBox1.Value = True Then interest = interest & " Sports " If CheckBox2.Value = True Then interest = interest & " Music " If CheckBox3.Value = True Then interest = interest & " Reading " MsgBox "Name: " & TextBox1.Text & vbCrLf & _ "Gender: " & gender & vbCrLf & _ "Interests: " & interest End Sub

3. Dynamic ComboBox Fill Example

जब Form लोड हो, ComboBox अपने आप Items से भर जाए।

Private Sub UserForm_Initialize() With ComboBox1 .AddItem "Math" .AddItem "Science" .AddItem "English" .AddItem "History" End With End Sub

4. Data Validation Example

TextBox खाली हो तो Submit न होने देना:

Private Sub CommandButton1_Click() If Trim(TextBox1.Text) = "" Then MsgBox "Name Field खाली नहीं हो सकता", vbExclamation TextBox1.SetFocus Exit Sub End If MsgBox "Data Submitted Successfully" End Sub

Final Tips

  • हर Control को meaningful Name दें जैसे txtName, cmbCourse, btnSubmit आदि।
  • Design करते समय Controls को Alignment और Spacing के साथ रखें ताकि Form देखने में साफ़ और प्रोफेशनल लगे।
  • हर CommandButton या Input Field की Proper Validation जरूर करें।
  • कोड हमेशा Proper Indentation के साथ लिखें जिससे समझना आसान हो।

Control Properties Summary Table

Control Property Description
TextBox Text User द्वारा डाला गया Data
Label Caption दिखने वाला Text
CommandButton Caption Button पर दिखने वाला Text
CheckBox Value True या False
OptionButton Value True या False (Only One Select)

FAQs

VBA Forms में सबसे अधिक उपयोग किए जाने वाले controls में TextBox, Label, CommandButton, ComboBox, ListBox, CheckBox और OptionButton शामिल होते हैं। ये सभी user से data लेने, दिखाने या action perform करने के लिए प्रयोग किए जाते हैं।
आप VBA Code में Control के नाम के बाद dot लगाकर उसकी किसी भी property को बदल सकते हैं, जैसे TextBox1.Text = "Hello" या CommandButton1.Caption = "Submit"
CommandButton का उपयोग किसी कार्य (जैसे - Data Submit करना, Form Reset करना या किसी calculation को run करना) को trigger करने के लिए किया जाता है। इस पर click करने पर Click event चलता है।
Events जैसे Click, Change, Enter आदि हमें user interaction के अनुसार customized actions define करने की सुविधा देते हैं जिससे Forms smart और responsive बनते हैं। उदाहरण: CommandButton पर click होने पर MsgBox दिखाना।
आप If condition और Trim function का उपयोग करके TextBox खाली है या नहीं यह जांच सकते हैं। उदाहरण: If Trim(TextBox1.Text) = "" Then MsgBox "Input जरूरी है"
CheckBox में आप multiple विकल्प चुन सकते हैं जबकि OptionButton (Radio Button) में एक समय में केवल एक ही विकल्प चुना जा सकता है। OptionButtons को Frame में group किया जाता है।

Please Give Us Feedback