Format Painter is triggering my function - format

My spreadsheet is designed such that whenever I update a field with a new value, a sub within my macro is called.
However, I discovered that whenever I apply the Format Painter to the field (strictly the Format Painter; not a value update), the sub is being called. I do not want the sub to be called in this scenario.
How can I code it so that the sub is only called on a value update, and not a formatting update?
The sub that is automatically being called is pasted below:
Private Sub Worksheet_Change(ByVal Target As Range)
CellChange Target ' Do validations etc.
End Sub

Related

Problem on applying script when i send the email

Does anyone know how to trigger VBA when i sent out an email.
I tried to look for the option from Rule Wizard on "Apply rule on message i send" but there is script option.
Cheers
Of course, you may assign a VBA sub to run when your rule is triggered. The sub should take a single argument which represents the item being sent:
Public Sub Test(item as Outlook.MailItem)
' run your code here
End Sub
You may add an event handler to the ItemSend event of the Application class. It is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks the Send button) or when the Send method for an Outlook item, such as MailItem, is used in a program.
Public WithEvents myOlApp As Outlook.Application
Public Sub Initialize_handler()
Set myOlApp = Outlook.Application
End Sub
Private Sub myOlApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim prompt As String
prompt = "Are you sure you want to send " & Item.Subject & "?"
If MsgBox(prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then
Cancel = True
End If
End Sub
You write an event procedure (also known as an event handler) to respond to events that occur in Microsoft Outlook. For example, you can write an event procedure that automatically maximizes the explorer window when Outlook starts.
Events are associated with particular objects. The Application object is the topmost object, and is always available (that is, it does not have to be created). You can add an Application event procedure in the ThisOutlookSession module window simply by selecting Application in the left list and then selecting the event in the right list.

How could I add events on dynamically-created array of controls?

First of all, I am using VB6, not VB.Net.
Now, I am trying to add events on dynamically-created array of controls. But when I try to add the WithEvents keyword to my array of controls, I get a "Compile Error: Expected: As" error.
Here's my code:
Option Explicit
Dim WithEvents btnNumbers() as CommandButton
Private Sub Form_Load()
ReDim btnNumbers(1 to 10) as CommandButton
Dim i as Integer
For i = 1 To UBound(btnNumbers) Then
Set btnNumbers(i) = Controls.Add("VB.CommandButton", "btnNumber" & i)
Set btnNumbers(i).Container = Form1
With btnNumbers(i)
'Properties of btnNumbers here...
End With
Next
End Sub
I want the buttons to display a message box that says it's number. Like for example when I clicked btnNumber4, a message box that says "I am Number 4!" to appear. I know how to do this message box part. The problem is that I can't add events to these buttons in order to do this.
Seeing that WithEvents doesn't seem to work, is there another way to add events to these buttons?
Thanks.

Add event listener to Excel Textbox (lose focus)

I need to fire an sub or a command when a user is done using a text box in Excel.
I have tried using the AfterUpdate() event and the LoseFocus() event like this:
Public Sub Kommentar_AfterUpdate()
MsgBox ("Hurray")
End Sub
The text box is named Kommentar and is inside the sheet Radio. Also, where is the code supposed to be written? i have tried placing it in code sheet for the Radio sheet, and in a separate module.
Any tip, hint or answer is appreciated!
For embedded ActiveX Excel control - add the following in the sheet mobile in VBA. TextBox1 is the name of the control:
Private Sub TextBox1_LostFocus()
End Sub
For userform - use the following where Textbox1 is the name of your textbox:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
End Sub
You can set the Cancel bool to True if you do not want to lose focus from the textbox. In addition the code is placed within the UserForm >> Right Click >> View Code.

The TextBox.text value is empty even though something has been entered

I am trying to debug a form window written in VB6. It is to enter customer data so you can type in an address in the address field. You can also type in something like 90210 Main Street and on enter it will automatically parse the text and write the 90210 in the postal code field below and let Main Street be in the address field. It however can occasionally parse it wrong, which is what I am trying to fix.
The problem is that I can't figure out how exactly it is set up. If I type something into the TextBox address field and do a
?ADDRESS.text
In the immediate window, the it returns an empty string. There is also only a single function defined when I look in the dropdown list under the form. But when I set a breakpoint at it and click the textbox, then it doesn't break. It is the GotFocus() event:
Private Sub ADDRESS_GotFocus()
Call GCui.BM(ADDRESS)
End Sub
It's the same with the POSTALCODE textbox. It has DblClick, GotFocus and LostFocus event functions defined. But setting a break point in either one of them has no effect.
Is there any way of finding out where in the form the value Main Street or 90210 is stored? They are clearly visible in the ADDRESS textbox and the POSTALCODE textbox, but the immediate window returns an empty line when asking for their values.
Update 1:
It seems that someone has decided to completely rebuild the form with new controls. It probably happens in form.load. But I would still like to know if there is a way to search through variable values to find the string "Main Street" or "90210".
Update 2:
It turns out that there are two frames on top of each other. The top frame is hidden at startup and the bottom (almost identical frame with same labels and controls) are shown.
You can use the "Watch" feature. This will let you inspect all the properties of a form and all controls within the form and their values (look at the controls node).
You can also do this via code by looping over the form.controls collection.
Dim o As Object
For Each o In Me.Controls
If TypeOf o Is TextBox Then
Debug.Print o.Text
End If
Next

Performing Form Validation with an Access 2003 ADP project

I'm developing an Access 2003 Database that uses a MS SQLServer backend.
I'm trying to do Form Validation and am experiencing some problems.
ValidationRule for each field seems to be ignored
I can't figure out what event I should override to enforce validation without having the database do it. (I'm not against this, it's just unknown to me how I'd catch Error Messages, instead of displaying them to the user)
I've tried getting around number 2 by disallowing closing and enforcing the use of a "Close Button", but the user can side step it by pressing tab, or by pressing the "Next Record" button at the bottom.
Any suggestions would be greatly appreciated.
If you are using the validation rules property, you can catch validation and duplicate key errors, amongst other things, in the Form Error event:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr=2107 Then
MsgBox "Validation error! " & ActiveControl.Name
End If
End Sub
You can enforce the use of your close button with a variable defined at form level and set to false unless your button is clicked.
Option Compare Database
Option Explicit
Public AllowClose As Boolean
Private Sub Form_Load()
AllowClose = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
Cancel = Not AllowClose
End Sub
Private Sub cmdClose_Click()
On Error GoTo Err_cmdClose_Click
AllowClose = True
DoCmd.Close
Exit_cmdClose_Click:
Exit Sub
Err_cmdClose_Click:
MsgBox Err.Description
Resume Exit_cmdClose_Click
End Sub
From: http://www.tek-tips.com/faqs.cfm?fid=2071
If you give us an example of one of your validation rules, it might help.
However, I've been under the habit of putting validation in Before Update (especially when I've got more complex validation algorithms)
Update:
I did some experimentation and it seems that the validation rule for the field is ONLY checked when you make an update to the field, but not if you are making an update somewhere else on the form, and not when you save the record. So I think your solution is to move validation to Form_BeforeUpdate.
OR you could make the column in the table non-null - which would force the users do something with the field.
OR, if the field must be "Testing", then you could side step the whole thing by setting a default value and locking the field so the user can't change it.
You can hide the navigation buttons by setting the form's Navigation Buttons property to "No." You can also disallow tabbing to a new record by setting the form's Cycle property to "Current Record."
With any nontrival application interface in Access, I usually lock the form down pretty tight and supply my own nav buttons, close button, etc. as needed.

Resources