Naming userform makes Textbox control malfunction

M

MarceepooNu

I posted this question yesterday (by mistake, I think) in
Outlook-Programming-VBA, at 11/16/2008 10:35 AM PST. I should have posted it
here, I think. If it is improper for me to post it here now, please don't
read any further and accept my apology.

The code below works fine in a form named "UserForm", but when I tried to run
the same code in another form (frmUserForm2's "Initialize" sub), nothing
appears in the textboxes.

I copied the sample code from the Outlook VBA help page named: "Cut, Paste
Methods, TextBox Control Example". I created two forms in my Outlook
VbaProject.Otm; one form was named: UserForm and the other was named
frmUserForm2, and I put the identical code in both, except for the name of
the form.

The sample code is set forth below, except that I changed the first line of
the code from...
Private Sub UserForm_Initialize()
to...
frmUserForm2
because I named the form: frmUserForm2

The code works fine in the form named "UserForm", but when I tried to run
the same code in frmUserForm2's "Initialize" sub, nothing appears in the
textboxes.
Question #1: I can't figure out why changing the name of the form prevents
the code from working.
Question #2: I have noticed that if I remove a form from the project, and
thereafter create a new form and give it the same name as the form that I
removed, I get an error message: "Path/file access error. Why does this
occur, and how can I fix it?


Private Sub frmUserForm2_Initialize()
TextBox1.Text = "From TextBox1!"
TextBox2.Text = "Hello "

CommandButton1.Caption = "Cut and Paste"
CommandButton1.AutoSize = True
End Sub

Private Sub CommandButton1_Click()
TextBox2.SelStart = 0
TextBox2.SelLength = TextBox2.TextLength
TextBox2.Cut

TextBox1.SetFocus
TextBox1.SelStart = 0

TextBox1.Paste
TextBox2.SelStart = 0
End Sub
 
E

Eric Legault [MVP - Outlook]

Hi MarceepooNu. The UserForm_Initialize event should never be renamed - it
doesn't have anything to do with the name of the form and is the same for
ALL forms.

As for the problem listed in Question 2 - very strange; I've never seen this
one before and I can't reproduce it.

BTW, you actually posted in the correct location before. Your topic is
definitely VBA related, while programming with custom forms primarily
involves using VBScript. The custom forms you are using are a variant of
the Windows Forms designed using Visual Studio (v6 anyway).

--
Eric Legault [MVP - Outlook]
MCDBA, MCTS (Messaging & Collaboration, SharePoint Infrastructure, MOSS 2007
& WSS 3.0 Application Development)
President, Collaborative Innovations
Proud new owner of OutlookCode.com
NEW! -> Try Picture Attachments Wizard 2.0 For Microsoft Outlook <-
Web: http://www.collaborativeinnovations.ca
Blog: http://blogs.officezealot.com/legault
....Take your SharePoint content offline...
 
M

MarceepooNu

Thank you.

MarceepooNu
--
MarceepooNu


Eric Legault said:
Hi MarceepooNu. The UserForm_Initialize event should never be renamed - it
doesn't have anything to do with the name of the form and is the same for
ALL forms.

As for the problem listed in Question 2 - very strange; I've never seen this
one before and I can't reproduce it.

BTW, you actually posted in the correct location before. Your topic is
definitely VBA related, while programming with custom forms primarily
involves using VBScript. The custom forms you are using are a variant of
the Windows Forms designed using Visual Studio (v6 anyway).

--
Eric Legault [MVP - Outlook]
MCDBA, MCTS (Messaging & Collaboration, SharePoint Infrastructure, MOSS 2007
& WSS 3.0 Application Development)
President, Collaborative Innovations
Proud new owner of OutlookCode.com
NEW! -> Try Picture Attachments Wizard 2.0 For Microsoft Outlook <-
Web: http://www.collaborativeinnovations.ca
Blog: http://blogs.officezealot.com/legault
...Take your SharePoint content offline...

MarceepooNu said:
I posted this question yesterday (by mistake, I think) in
Outlook-Programming-VBA, at 11/16/2008 10:35 AM PST. I should have posted
it
here, I think. If it is improper for me to post it here now, please don't
read any further and accept my apology.

The code below works fine in a form named "UserForm", but when I tried to
run
the same code in another form (frmUserForm2's "Initialize" sub), nothing
appears in the textboxes.

I copied the sample code from the Outlook VBA help page named: "Cut, Paste
Methods, TextBox Control Example". I created two forms in my Outlook
VbaProject.Otm; one form was named: UserForm and the other was named
frmUserForm2, and I put the identical code in both, except for the name of
the form.

The sample code is set forth below, except that I changed the first line
of
the code from...
Private Sub UserForm_Initialize()
to...
frmUserForm2
because I named the form: frmUserForm2

The code works fine in the form named "UserForm", but when I tried to run
the same code in frmUserForm2's "Initialize" sub, nothing appears in the
textboxes.
Question #1: I can't figure out why changing the name of the form
prevents
the code from working.
Question #2: I have noticed that if I remove a form from the project, and
thereafter create a new form and give it the same name as the form that I
removed, I get an error message: "Path/file access error. Why does this
occur, and how can I fix it?


Private Sub frmUserForm2_Initialize()
TextBox1.Text = "From TextBox1!"
TextBox2.Text = "Hello "

CommandButton1.Caption = "Cut and Paste"
CommandButton1.AutoSize = True
End Sub

Private Sub CommandButton1_Click()
TextBox2.SelStart = 0
TextBox2.SelLength = TextBox2.TextLength
TextBox2.Cut

TextBox1.SetFocus
TextBox1.SelStart = 0

TextBox1.Paste
TextBox2.SelStart = 0
End Sub
 

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