userform losing entered data

R

red6000

Hi,

I have a set up using various userforms.

The userforms contain various textboxes which then feed the code:

Selection.TypeText Text:= Userform1.TextBox1.value

However when my code runs, it is not returning the value entered into the
textbox and instead it types nothing.

Now on face value, it would suggest that I either have an incorrect
reference to the textbox or somewhere I have coded 'unload userform1' so
that it empties the data entered.

However, the problem is intermittent. Sometimes it types the value of the
textbox and othertimes its just blank (the majority of the time it works).
I have ensured that when testing, I am using exactly the same inputs, so I
am 100% sure it is not the code as it is exactly the same senario that I am
testing.

So is there any 'bug' that can intermitently lose the data of a userform
from the memory?

Has anyone ever experienced such a situation?

Thanks.
 
D

Doug Robbins - Word MVP

I would suggest that you rename your controls so that their names have some
meaning and also that instead of using the Selection object, you either have
the data inserted into the range of a bookmark or have is loaded into a
document variable and have it display in the document by means of a
docvariable field.

The reason you code is working intermittently at the moment is probably
because the selection is not always were you intend it to be.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

red6000

thanks for the reply Doug,

the intermittent problem also happens when build a string, eg:

Dim xFileName as String
xFileName = UserForm1.TextBox1.Value & " some text"
ActiveDocument.SaveAs filename:=xFileName

The document is saving as simply " some text", but more often that not it
does actually save with value of the textbox in the filename. This is what
I just dont understand.
 
D

Doug Robbins - Word MVP

Where do you actually have this code? If it is in the userform itself,
there is no need to prefix the name of the control with the name of the
userform. Just

xFileName = TextBox1.Text & " some text"

would do. Note, for a textbox, I would use the .Text property rather than
the .Value property.


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

red6000

Thanks for the tips, my actual code is held in modules as opposed to the
forms so that I can re-use the code in various parts of the program.

Is holding the code in modules going to impact it at all?

Thanks for the help.
 
D

Doug Robbins - Word MVP

Well, it seems like doing it that way, the code is intermittently losing the
reference to the form on which the control is located.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

red6000

I think I've narrowed down the code where seems to be wiping the text
entered in the text box.

The code in the module is below. The control that is losing the data is
'ParaChooserFrm.account'. Thanks for the help.

Function SaveTempCopy(xName As String)

Dim savedname As String

savedname = ParaChooserFrm.account.Value & " - " & xName & " - Date - "
& Day(Date) & " " & Month(Date) & " " & Year(Date) & " - Time - " &
Hour(Time) & "hr " & Minute(Time) & "mins " & second(Time) & "secs" & ".doc"

ChangeFileOpenDirectory _
"J:\TempStorage\"

ActiveDocument.SaveAs filename:=savedname, WritePassword:="",
ReadOnlyRecommended:=False
ActiveDocument.Close savechages = True

Set cb = EditFrm.Controls.Add("Forms.checkbox.1")
cb.Caption = xName
cb.ControlTipText = savedname

Set lb = LocalFrm.Controls.Add("Forms.checkbox.1")
lb.Caption = xName
lb.ControlTipText = savedname

Set eb = emailFrm.Controls.Add("Forms.checkbox.1")
eb.Caption = xName
eb.ControlTipText = savedname
If xName <> "Agt" And xName <> "GPR1" And xName <> "GPR2" And xName <>
"LDR1" And xName <> "LDR" Then
eb.Enabled = False
End If

End Function
 

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