creating and saving data from a userform

B

bluewatermist

Hello

I'm totally at a loss as to how I can achieve this, but this site has helped
me in the pass and I'm hoping you will be able to help me now.

I have an excel program which stays in a share drive. Staff are then able
to open up the excel program which is all VB. The first form they encounter
is to setup their signature block. There are four textboxes, Eg: name, state,
telephone etc. Currently when exiting out of this program their signature
block is not saved. I'm hoping to create a text document via notepad or
similiar file which would save their signature block in their own drive.

I thought of creating two command buttons, one to save and another to update
if needed. The save button would check in their own drive if there was such
as file (eg: signatureblock.txt) and if not would create on. The update
button would again check if there was such a file, it would either delete the
old one and then create a new one or create a new one.

I was hoping that next time that staff member opened the program, the
program would automatically extract the signature block information and place
this info appropriately in the correct textboxes.

I'm hoping for any guidance or assistance.
Many thanks in advance.
 
M

Mike H

Hi,

This uses the activate and terminate userform events. It will read or create
a text file that holds the text in textbox1 on the userfrom and automatically
populate that into the textbox if the file exists.

Private Sub UserForm_Activate()
On Error Resume Next
Dim FileNum As Integer
Close
FileNum = FreeFile
Open "c:\nametext.txt" For Input As #FileNum
Input #FileNum, usrname
Close
TextBox1.Text = usrname
End Sub


Private Sub UserForm_Terminate()
Dim FileNum As Integer
Close
FileNum = FreeFile
Open "c:\nametext.txt" For Output As #FileNum
Write #FileNum, TextBox1.Text
Close
End Sub

Mike
 
B

bluewatermist

Hi Mike

Thank you for such a quick reply. For the first textbox it works but i
can't seem to add several textboxes after it. Can you suggest anything.

many thanks
 
M

Mike H

Hi,

It all starts getting a bit messy because i don't fully understand what you
tring to do but this now writes multiple values to the data file and reads
them back in again when the userfom loads.

the way it works is that the text file is comma delimited and looks
something like this

"Mike","In","England"

Private Sub UserForm_Activate()
On Error Resume Next
Dim FileNum As Integer
Close
FileNum = FreeFile
Open "c:\nametext.txt" For Input As #FileNum
Input #FileNum, usrname, Address, somethingelse
Close
TextBox1.Text = usrname
TextBox2.Text = Address
TextBox3.Text = somethingelse
End Sub

Private Sub UserForm_Terminate()
Dim FileNum As Integer
Close
FileNum = FreeFile
Open "c:\nametext.txt" For Output As #FileNum
Write #FileNum, TextBox1.Text, TextBox2.Text, TextBox3.Text
Close
End Sub


Mike
 
B

bluewatermist

Hi Mike

You're brilliant! That's exactly what I needed. Thank you so much for your
support.

Best Regards
 

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