Go Back   Access World Forums > Microsoft Access Discussion > Modules & VBA

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 06-19-2002, 07:00 AM   #1
alguzman
Member
 
Join Date: Aug 2001
Location: Long Branch, NJ
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
alguzman
Question coding command button

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

alguzman is offline   Reply With Quote
Old 06-19-2002, 07:16 AM   #2
David R
I know a few things...
 
David R's Avatar
 
Join Date: Oct 2001
Location: Kansas City, MO, USA
Posts: 2,628
Thanks: 86
Thanked 149 Times in 148 Posts
David R is on a distinguished road
How about...

Code:
Else
 Exit Sub 'do nothing
David R is offline   Reply With Quote
Old 06-19-2002, 07:24 AM   #3
alguzman
Member
 
Join Date: Aug 2001
Location: Long Branch, NJ
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
alguzman
I tried that and does not work. It will still run and set the font to bold.

alguzman is offline   Reply With Quote
Old 06-19-2002, 07:31 AM   #4
David R
I know a few things...
 
David R's Avatar
 
Join Date: Oct 2001
Location: Kansas City, MO, USA
Posts: 2,628
Thanks: 86
Thanked 149 Times in 148 Posts
David R is on a distinguished road
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.
David R is offline   Reply With Quote
Old 06-19-2002, 07:38 AM   #5
alguzman
Member
 
Join Date: Aug 2001
Location: Long Branch, NJ
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
alguzman
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
alguzman is offline   Reply With Quote
Old 06-19-2002, 07:48 AM   #6
David R
I know a few things...
 
David R's Avatar
 
Join Date: Oct 2001
Location: Kansas City, MO, USA
Posts: 2,628
Thanks: 86
Thanked 149 Times in 148 Posts
David R is on a distinguished road
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.
David R is offline   Reply With Quote
Old 06-19-2002, 08:49 AM   #7
alguzman
Member
 
Join Date: Aug 2001
Location: Long Branch, NJ
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
alguzman
Thumbs up

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.


alguzman is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump




All times are GMT -8. The time now is 08:55 AM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World