Buttons and Label

S

Sekker

I am quite new to VBA so please bare with me. I am trying to figure ho
to build a label by concatenating strings using command buttons.

For example pressing command button 1 scrolls through a list of option
and displays them in the label

Command button 2 scrolls through choices applicable to displayed tex
from button1 to add to the label.

I have tried two methods using boolean and arrays but I have not bee
that successful.

Could someone look at the code below and let me know if I am on th
right track. Any suggestions or better methods would be greatl
appreciated

Thanks

Sekker

Version 1

Option Explicit

Dim garage As Boolean
Dim lights As Boolean
Dim temp As Boolean
Dim phone As Boolean
Dim security As Boolean
Dim ButtonOne As String
Dim ButtonTwo As String
Dim garageopt As Boolean
Dim lightsopt As Boolean
Dim tempopt As Boolean

Sub DisplayCaption()
' concatenate the caption with the two string
' variables.
Label1.Caption = ButtonOne & " " & ButtonTwo
End Sub

Private Sub CommandButton1_Click()

If garage Then
ButtonOne = "garage"
garage = False
lights = True
temp = False
' phone = False
' security = False
Call DisplayCaption

ElseIf lights Then
ButtonOne = "lights"
garage = False
lights = False
temp = True
' phone = False
' security = False
Call DisplayCaption

Else
ButtonOne = "temp"
garage = True
lights = False
Call DisplayCaption

End If
End Sub

Private Sub UserForm_Initialize()
garage = False
garageopt = False
End Sub

Private Sub CommandButton2_Click()

If garage And garageopt Then
ButtonTwo = "Open"
garageopt = False

ElseIf garage And garageopt = False Then
ButtonTwo = "Close"
garageopt = True

End If

End Sub

Private Sub CommandButton3_Click()

End Sub

Private Sub Label1_Click()

End Sub



Version 2

Option Explicit
Dim ArrayOption(1 To 5) As String
Dim Flag As Integer
Dim Index As Integer
Private Sub CmdOption_Click()
FillArray
DisplayArray




End Sub

Private Sub CmdSet_Click()
Flag = 0

End Sub

Private Sub FrmDisplay_Click()
TextBox1.SetFocus

End Sub

Private Sub mdReset_Click()
'clear the textbox
TextBox1.Text = ""
End Sub

Private Sub TextBox1_Change()

End Sub



Private Sub UserForm_Click()

End Sub


Public Sub DisplayArray()
Dim Counter As Integer
Counter = 1
Flag = 1

Do While DoEvents()
If Flag = 1 Then
TextBox1.Text = ArrayOption(Counter)
Counter = Counter + 1
If Counter = 5 Then
Counter = 1
End If
End If
Loop



End Sub

Public Sub FillArray()
ArrayOption(1) = "Garage"
ArrayOption(2) = "Lights"
ArrayOption(3) = "Security"
ArrayOption(4) = "Temp"
ArrayOption(5) = "Messages"
End Su
 
D

Doug Robbins - Word MVP

I haven't tried to completely understand your code, but it seems to me that
what you want to do could be achieved by have a combobox loaded with the
first lot of items and then depending upon what was chosen from that
combobox, you would populate a second combobox with items relating to the
chosen item in the first combobox. You would then concatenate the two items
chosen from the comboboxes. Does that sound right?

If so, here's a bit of code that I supplied to a poster in the userforms
newsgroup:

Private Sub UserForm_Initialize()
Dim sourcedoc As Document, i As Long, myitem As Range
' Modify the path in the following line so that it matches where you saved
the data document
Set sourcedoc = Documents.Open(FileName:="j:\drive
D40\Documents\Philsdata.doc")
For i = 2 To sourcedoc.Tables(1).Rows.Count
Set myitem = sourcedoc.Tables(1).Cell(i, 1).Range
myitem.End = myitem.End - 1
cmbCompany.AddItem myitem.Text
Next i
sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
End Sub

Private Sub cmbCompany_Change()
' This code populates the Profile combobox with the relevant data
' based on the company that has been selected.
Dim sourcedoc As Document, i As Long, myitem As Range
'Clear any data from the comboboxes
cmbProfile.Clear
' Modify the path in the following line so that it matches where you saved
the data document
Set sourcedoc = Documents.Open(FileName:="j:\drive
D40\Documents\Philsdata.doc")
'Populate the Profile combo
For i = 1 To sourcedoc.Tables(1).Cell(cmbCompany.ListIndex + 2,
2).Range.Paragraphs.Count
Set myitem = sourcedoc.Tables(1).Cell(cmbCompany.ListIndex + 2,
2).Range.Paragraphs(i).Range
myitem.End = myitem.End - 1
cmbProfile.AddItem myitem.Text
Next i
sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
End Sub

For this to work, you have a two column table in the source document with
the entries for the first combobox in the cells in the first column of the
table and the entries for the second combo box, corresponding to each item
in the first combobox as separate paragraphs in the cell in column2

For example

CompanyA ProfileA1
ProfileA2
ProfileA3
CompanyB ProfileB1
ProfileB2
ProfileB3

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top