PDA

View Full Version : [SOLVED] forms and modules



northernstar
10-07-2007, 12:35 AM
good morning

i have a multipage form which i thought was getting quite clogged down with code so i decided to put some of the code into its own module to keep everything sperate and easier to maintain (also meant i could run the code for more than one task - click or update etc)

but the individual code doesnt appear to be recognising the names of textboxes and just passing over the if statements even if they are true

the file can be found via the link below (password if needed is 2729)
http://www.vbaexpress.com/forum/showthread.php?t=15375

thanks

Bob Phillips
10-07-2007, 12:50 AM
Qualify the texboxes with the name of the form.

northernstar
10-07-2007, 01:43 AM
sorry for neing stupid but exactly what do you mean by qualify?

would that be for textbox in the form named nominal1

dim nominal1
or
dim nominal1 as textbox
or
dim nominal1 as something else

please dont hold it against me for being a little green when it comes to stuff like this

thanks again

northernstar
10-07-2007, 01:47 AM
i get the error message incorrect use of Me keyword??


Private Sub test()
dim a as textbox
For i = 1 To 6
If dp = "" Then
Exit Sub
End If
If dp = 0 Then
Me.Controls("a" & i).Text = Format(a & i, "0")
Else
Me.Controls("a" & i).Text = Format(a & i, "0." & String(dp, "0"))
End If
Next i
End Sub


i have am using a sample file to get the code to work rather than using my actual file and this is in its own module

any ideas?

thanks again

Bob Phillips
10-07-2007, 02:01 AM
Not me, that applies to the container, and you are not in the container. Use the name of the form.

northernstar
10-07-2007, 02:12 AM
ok will give that ago

cheers and hopefully i wont have to bother you for a while now

thanks again

northernstar
10-07-2007, 02:15 AM
spoke to soon

just tried it and i am not getting any errors up but it aint formatting the cells at all?

Bob Phillips
10-07-2007, 02:31 AM
Two things?

Do you use Option Explicit?

What is dp?

northernstar
10-07-2007, 02:42 AM
yes i use Option Explicit and dp is the name of the textbox which determines the number of decimal places fir the other textboxes

Bob Phillips
10-07-2007, 02:49 AM
You have to qualify that as well then.

Bob Phillips
10-07-2007, 02:52 AM
Private Sub test()
With UserForm1
For i = 1 To 6
If .dp.Text = "" Then
Exit Sub
End If
If .dp.Text = "0" Then
.Controls("a" & i).Text = Format(.Controls("a" & i).Text, "0")
Else
.Controls("a" & i).Text = Format(.Controls("a" & i).Text, "0." & _
String(CLng(.dp.Text), "0"))
End If
Next i
End With
End Sub

northernstar
10-07-2007, 02:56 AM
Option Explicit

Private Sub test()
Dim i As Integer
Dim a As TextBox
Dim dp As Integer
For i = 1 To 6
'If dp = "" Then
'Exit Sub
'End If
If dp = 0 Then
UserForm1.Controls("a" & i).Text = Format(a & i, "0")
Else
UserForm1.Controls("a" & i).Text = Format(a & i, "0." & String(dp, "0"))
End If
Next i
End Sub

this is the code have just tried

Gert Jan
10-07-2007, 03:32 AM
this is the code have just tried

And with what result? I'm pretty sure it failed, the last code from xld works (when using Option Eplicit you'll have to declare i).
You are declaring dp as an integer, (why? it is a textbox) and then do not assign a value to it. that's not going to work.

northernstar
10-07-2007, 03:34 AM
just tried this and if i put a 1 into a1 and 1 in to dp then press format then it fills in the other textboxes 2, 3, 4, 5 and 6??


Dim i
Dim a
Dim dp
For i = 1 To 6
'If dp = "" Then
'Exit Sub
'End If
If dp = 0 Then
UserForm1.Controls("a" & i).Text = Format(a & i, "0")
Else
UserForm1.Controls("a" & i).Text = Format(a & i, "0." & String(dp, "0"))
End If
Next i
End Sub

Bob Phillips
10-07-2007, 03:35 AM
Look at the code I gave you in #11!

northernstar
10-07-2007, 03:40 AM
what is #11?

northernstar
10-07-2007, 03:45 AM
just worked ou what #11 is
didnt actual see this reply pc crashed and i just missed it

cheers that seems to be working great down

sorry for being a pain

Bob Phillips
10-07-2007, 03:45 AM
Post number 11 in this thread.

northernstar
10-07-2007, 09:47 AM
solved the original problem now

thanks to all those helped me

thanks again