VBA UserForm Coding Assistance

S

Shauna Koppang

This is a continuation from my previous message with added
information.

I am a rank beginner with VBA and this is my firt attempt
at a UserForm. I need to eventually create a dialog box
that users can either check, type in or pick from a list,
selected items to complete a template. So I am
experimenting with now creating a Userform with each of
the items on it. Here is the code so far. After I will
explain where I am still having troubles:
Private Sub UserForm_Initialize()
'Handling ListBox list
ListBox1.List = Array("By Mail", "By Mail & Fax", "By
Mail & E-mail", "By Fax & E-mail", "By Hand", "By
Courier", "By Special Delivery", "By Registered Mail")
ListBox1.ListIndex = 0

'Sensitivity ComboBox list
ComboBox1.AddItem "Personal"
ComboBox1.AddItem "Private"
ComboBox1.AddItem "Confidential"
ComboBox1.AddItem "Personal & Confidential"
ComboBox1.AddItem "Private & Confidential"

'Use drop-down list
ComboBox1.Style = fmStyleDropDownList
'Combo box values are ListIndex values
ComboBox1.BoundColumn = 0
'Set combo box to first entry
ComboBox1.ListIndex = 0
End Sub
Private Sub CommandButton1_Click()

'Handling List Box
If ListBox1.ListIndex >= 0 Then
With ActiveDocument
.Bookmarks("Text1").Range.InsertBefore
ListBox1.Text
End With
Else
MsgBox "You must select an item."
End If

'Sensitivity ComboBox
If ComboBox1.ListIndex >= 0 Then
With ActiveDocument
.Bookmarks("Text2").Range.InsertBefore
ComboBox1.Text
End With
Else
MsgBox "You must select or type an item."
End If

'Text Box
With ActiveDocument
.Bookmarks("Text3").Range.InsertBefore TextBox1
'If Len(.Result) = 0 Then
' MsgBox "You must fill in this field"
'End If
End With

UserForm1.Hide

End Sub

Help with:

The ComboBox works to pick items from it and it places the
selecting into the bookmark, however, I cannot figure out
how to get it so I can type in it then get the result to
be placed at that bookmark.

The TextBox will appear and I can type in and it will
place the result into the bookmark, however, the code (now
commented out) for an error message if not filled in, does
not seem to work.

Have not tried a check box yet or radio button! That's
next. Yikes.

I appreciate your patience with this and your assistance.

Shauna
 
M

Martin Sägesser

Hi Shauna

'Use drop-down list
ComboBox1.Style = fmStyleDropDownList

why?, without you can edit your ComboBox!


Private Sub CommandButton1_Click()

'Text Box
With ActiveDocument
.Bookmarks("Text3").Range.InsertBefore TextBox1
'If Len(.Result) = 0 Then
' MsgBox "You must fill in this field"
'End If
End With

use instead:

If Len(TextBox1) = 0 Then
MsgBox You must fill in this field"
Exit Sub
Else
With ActiveDocument.Bookmarks("Text3").Range.InsertBefore TextBox1
End If

UserForm1.Hide

use 'Unload Me', with Hide the Form is only 'hidden' ;-)
Have not tried a check box yet or radio button! That's
next. Yikes.

good luck ... an ask us again!

hth, Martin
 
S

Shauna Koppang

HI Martin,

I tried and I guess I am just too new to know if it is
right. I can get it to give me an error message but it
will not halt after the Command Button click with the
other routines running to the user complete an entry.

I modified the code your sent a number of times and this
what I am back to. Would really appreciate if you could
assist in straightening me out on this. Thanks so much!

Text Box
With OriginalDoc
If Len(TextBox1) = 0 Then
MsgBox "You must fill in a Sender Name"
Else
.Bookmarks("Text3").Range.InsertBefore
TextBox1
End If
End With
Shauna
-----Original Message-----
Hi Shauna



why?, without you can edit your ComboBox!




use instead:

If Len(TextBox1) = 0 Then
MsgBox You must fill in this field"
Exit Sub
Else
With ActiveDocument.Bookmarks
("Text3").Range.InsertBefore TextBox1
 
S

Shauna Koppang

Hi Martin,

This response is about the Combo Box.

I changed the code as below (actually commented it out)
and it lets me type in something into the Combo Box but
does not put it into the Bookmark in the document. Am I
missing a step in either the initialization code or the
below coding?

Here is the initialization part of the Combo Box:

'Sensitivity ComboBox list
ComboBox1.AddItem "Personal"
ComboBox1.AddItem "Private"
ComboBox1.AddItem "Confidential"
ComboBox1.AddItem "Personal & Confidential"
ComboBox1.AddItem "Private & Confidential"

'Use drop-down list
'ComboBox1.Style = fmStyleDropDownList
'Combo box values are ListIndex values
ComboBox1.BoundColumn = 0
'Set combo box to first entry
ComboBox1.ListIndex = 0

Here is the Insertion part of the Combo Box

'Sensitivity ComboBox
If ComboBox1.ListIndex >= 0 Then
With OriginalDoc
.Bookmarks("Text2").Range.InsertBefore
ComboBox1.Text
End With
Else
MsgBox "You must select or type an item."
End If
Thanks again for all your assistance :)

Shauna
-----Original Message-----
Hi Shauna



why?, without you can edit your ComboBox!




use instead:

If Len(TextBox1) = 0 Then
MsgBox You must fill in this field"
Exit Sub
Else
With ActiveDocument.Bookmarks
("Text3").Range.InsertBefore TextBox1
 

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