In current-build Access 365, I'm using an ADO parameterized query to insert data into an emails table. When attempting to do so, one of the validation rules in the table appears to always fail. When manually entering the same data into the same field, the validation rule appears to behave properly. I'm really scratching my head over this one, so hopefully someone here can spot what I couldn't.
For context, the table in question (tblSingleEmails) is just a contact list for one-off emails in relation to a given project. tblSingleEmails consists of an autonumber primary key (singleEmailID), a foreign key (projectID), and two fields - emailUser and emailDomain. There are various reasons for splitting the full email address in this way, but I won't go into them here.
Both emailUser and emailDomain have a Validation Rule and Validation Text set up. These rules are really just there to ensure during data entry users understand what they're supposed to enter in each field. For emailUser, it's:
For emailDomain:
Code:
(Not Like "*@*") And (Like "*.*")
Not nearly enough to validate the entry as correct - but I have out-of-scope VBA to handle that. This just ensures the user is entering something that looks like an email username and domain name.
I'm using a class called SqlCommand to create my parameters for me, as defined here: http://codereview.stackexchange.com/...ers-on-the-fly
That class is pretty well validated for this use-case - I've used it in numerous places throughout my code without any trouble so far. I therefore don't think there is an issue with it, although I did do a bit of testing to confirm that, which I'll discuss later. I call that class from a function in a standard module where I'm attempting to insert some test email values. The relevant module code:
Code:
Public Function AddEmails(ByVal lProjectID As Long, ParamArray EmailValues() As Variant) As Boolean
...
Dim sSQL As String, lIndex As Long, objConn As ADODB.connection
35 sSQL = "INSERT INTO tblSingleEmails (projectID, emailUser, emailDomain) VALUES (?, ?, ?);"
40 Set objConn = CurrentProject.connection
...
60 For lIndex = LBound(EmailValues()) To UBound(EmailValues())
Dim bResult As Boolean, objCmd As New SqlCommand, sUser As String, sDomain As String
65 sUser = Left(EmailValues(lIndex ), InStr(1, EmailValues(lIndex ), "@") - 1)
70 sDomain = Right(EmailValues(lIndex ), Len(EmailValues(lIndex )) - InStr(1, EmailValues(lIndex ), "@"))
75 bResult = objCmd.ExecuteNonQuery(objConn, sSQL, lProjectID, sUser, sDomain)
80 If Not bResult Then Err.Raise FunctionErr.InsertQueryFailed, sModName & "." & sProcName, "Failed to insert single email."
85 Next lIndex
...
End Function
Here's the exact code called by objCmd.ExecuteNonQuery:
Code:
Public Function ExecuteNonQuery(connection As ADODB.connection, sql As String, ParamArray parametervalues()) As Boolean
Dim cmd As New ADODB.Command
cmd.ActiveConnection = connection
cmd.CommandType = adCmdText
cmd.CommandText = sql
Dim i As Integer
Dim value As Variant
For i = LBound(parametervalues) To UBound(parametervalues)
value = parametervalues(i)
cmd.parameters.Append ToSqlInputParameter(value)
Next
On Error Resume Next
cmd.Execute
ExecuteNonQuery = (Err.Number = 0)
On Error GoTo 0
End Function
I've paused execution before cmd.Execute and confirmed that cmd.Parameters(2) = "gmail.com", so the parameterization is working correctly. Comparing that to cmd.CommandText, it's clear that it is attempting to insert "gmail.com" into the emailDomain field of tblSingleEmails, all as intended, as cmd.CommandText is properly populated with the sSQL string. I also tested in the immediate pane:
Code:
debug.Print Not ("gmail.com" Like "*@*") And ("gmail.com" like "*.*")
This returns True, confirming that it should be passing validation. I've also manually entered my test data into the table, and the validation rules have performed as expected. emailDomain is perfectly happy with "gmail.com" when manually entered, and won't accept "gmailcom" or "gmail@com.com" -- all exactly as expected.
However, running cmd.Execute results in the aforementioned error with error description equal to my Validation Text for the emailDomain field. I feel like I must be missing something obvious, but I don't see it. Any help would be greatly appreciated.