How do I Initialize Form?

T

TJDeborah

I have a very simple form that inserts data into a simple spreadsheet
and it all works right now. However I want my command button that
inserts the data to initialize ( or clear all of the fields) once the
data has been place in the spreadsheet. I have a command button that
clears all of the fields but would really like it to on submit as well.
I just can't seem to get the logic correct. Here is my code for those
2 command buttons.

Private Sub cmdClearForm_Click()

Call UserForm_Initialize

End Sub

Private Sub cmdOK_Click()

ActiveWorkbook.Sheets("Data").Activate

Range("A1").Select

Do

If IsEmpty(ActiveCell) = False Then

ActiveCell.Offset(1, 0).Select

End If

Loop Until IsEmpty(ActiveCell) = True

ActiveCell.Value = txtdatee.Value

ActiveCell.Offset(0, 1) = txtjob.Value

ActiveCell.Offset(0, 2) = cboOperator.Value

ActiveCell.Offset(0, 3) = cboOperation.Value

ActiveCell.Offset(0, 4) = cboDescription.Value

ActiveCell.Offset(0, 5) = txtThickness.Value

ActiveCell.Offset(0, 6) = txtLength.Value

ActiveCell.Offset(0, 7) = txtQty.Value

ActiveCell.Offset(0, 8) = txtTime.Value


Range("A1").Select


End Sub
 
G

Gary Keramidas

i haven't tested any of your code, don't see your end do. but right after you
insert the data, and set the form fields to ""

for example:

txtdatee.Value = ""

txtjob.Value = ""

cboOperator.Value = ""

'continue with the rest of the form fields

range("a1").select
 
G

Gary Keramidas

here's some code i use as an example:

the 1st loop writes the data to the sheet, the 2nd loop clears the data.

the Me.controls equate to T1aScore and T1bScore thru T12aScore and T12bScore,
which are my textbox names

'write the data
n = 9
For z = 1 To 12
wks.Cells(n, Me.WeekNum + 1).Value = Me.Controls("T" & z & "aScore").Value
n = n + (n Mod 3) + 1
wks.Cells(n, Me.WeekNum + 1).Value = Me.Controls("T" & z & "bScore").Value
n = n + (n Mod 3) + 1
Next

'clear the form
For z = 1 To 12
Me.Controls("T" & z & "aScore").Value = ""
Me.Controls("T" & z & "bScore").Value = ""
 
T

TJDeborah

You are right there is no end do....but it works until i try to put in
something that called the initialized form like on the clear form
button. I will try what you suggested.
 
T

Tim Williams

Just add

Call UserForm_Initialize

as the last line in your cmdOK_Click() procedure.

Tim
 
T

TJDeborah

Tim, that worked!!! I want to say I tried that but if I had well ya
know. I have been trying to learn VBA on my own this week.

THANKS!!!
 

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