coding command button (1 Viewer)

alguzman

Registered User.
Local time
Today, 03:05
Joined
Aug 2, 2001
Messages
63
I'm trying to code a command button so that when pressed you will see a input box that will ask you if you want to change the font to bold or italic. I have that working but if you do not type nothing and hit ok it will bold everything. What am I missing from my code. If you do not type nothing in I do not want the code to run. Here is my code: Any help would be great. Thanks

Private Sub Command27_Click()
Dim strInput As String
Dim strAnswer As String
Dim frmCurrent As Form
Dim ctlControl As Control

On Error GoTo Command27_Err

strAnswer = InputBox("Enter Bold or Italic to channge the font", "Font Change")
For Each frmCurrent In Forms
For Each ctlControl In frmCurrent.Controls
If strAnswer = Left$(strAnswer, 4) Then
ctlControl.FontBold = True
Else
If strAnswer = Left$(strAnswer, 6) Then
ctlControl.FontItalic = True
Else
'do nothing
End If
End If
Next
Next

command27_Exit:
Exit Sub
Command27_Err:
If Err = 438 Then
Resume Next
Else
msgbox Error$
Resume command27_Exit
End If

End Sub:confused:
 

David R

I know a few things...
Local time
Yesterday, 21:05
Joined
Oct 23, 2001
Messages
2,633
How about...

Code:
Else
 Exit Sub 'do nothing
 

alguzman

Registered User.
Local time
Today, 03:05
Joined
Aug 2, 2001
Messages
63
I tried that and does not work. It will still run and set the font to bold.
 

David R

I know a few things...
Local time
Yesterday, 21:05
Joined
Oct 23, 2001
Messages
2,633
Have you considered actually checking that strAnswer is "bold" or "italic", rather than just how long the string is? I've never seen that particular method before.
 

alguzman

Registered User.
Local time
Today, 03:05
Joined
Aug 2, 2001
Messages
63
David R, thanks for your help. I'm new to VBA and I'm practicing with the book "Beginning Access 97 VBA Programming". I created a database at work and added code here and there to make it somewhat automated by searching this website and other Access sites. I finally decided to try and learn VBA for myself and write my own code. Anyway I found my problem and fix it. I know this might not be the best way to do it, but as I keep reading and prcaticing my coding will get better---I hope..lol

Here is the final code:

Private Sub Command27_Click()
Dim strInput As String
Dim strAnswer As String
Dim frmCurrent As Form
Dim ctlControl As Control

On Error GoTo Command27_Err

strAnswer = InputBox("Enter Bold or Italic to channge the font", "Font Change")
For Each frmCurrent In Forms
For Each ctlControl In frmCurrent.Controls
If Len(strAnswer) = 4 Then
ctlControl.FontBold = True
Else
If Len(strAnswer) = 6 Then
ctlControl.FontItalic = True
Else
GoTo command27_Exit:
End If
End If
Next
Next
command27_Exit:
Exit Sub
Command27_Err:
If Err = 438 Then
Resume Next
Else
msgbox Error$
Resume command27_Exit
End If

End Sub
 

David R

I know a few things...
Local time
Yesterday, 21:05
Joined
Oct 23, 2001
Messages
2,633
Here's what I'm suggesting

Instead of If Len(strAnswer) = 4 Then, use
Code:
If strAnswer = "bold" Then
This will make your code more precise, and arguably a little faster. As it stands presently, I could enter "xyza" and get it to bold my code, since the word is four letters long.
 

alguzman

Registered User.
Local time
Today, 03:05
Joined
Aug 2, 2001
Messages
63
thanks David, that does work better and you are right about using any string with four letters or six. The chapter I just finished reading was dealing with left$, Right$, MID, and LEN maybe that is way I was trying to use it. Thanks for your help.;)
 

Users who are viewing this thread

Top Bottom