|
-
May 15th, 2007, 12:44 PM
#1
Thread Starter
Hyperactive Member
NULL dates are driving me nuts
Well not really but......
I have an insert statement that looks mostly like this.....
Code:
mySQL = "INSERT INTO myTABLE (someDumbDate) VALUES (#" & aTextBox.text & "#)"
This works fine when aTextBox.text has a real date in it but is causing me unspeakable grief when aTextBox.text = "".
Is there an easy way around this?
Database: MS Access 2003
Language: Visual Basic (VS 2005)
-
May 15th, 2007, 12:51 PM
#2
Re: NULL dates are driving me nuts
You can't use blank as a Null in an SQL statement, you need to explicitly specify it, eg:
-
May 15th, 2007, 01:07 PM
#3
Re: NULL dates are driving me nuts
You can also do
Code:
If aTextBox.Text <> vbNullString Then
mySQL = "INSERT INTO myTABLE (someDumbDate) VALUES (#" & aTextBox.text & "#)"
End If
-
May 15th, 2007, 02:18 PM
#4
Thread Starter
Hyperactive Member
Re: NULL dates are driving me nuts
Hi Hack and Si;
I was expecting those answers so it looks like workaround time has arrived.
Thanks guys.
-
May 15th, 2007, 02:22 PM
#5
Re: NULL dates are driving me nuts
I normally do this if a date value can be null (like a closed date):
Code:
Dim strCDate As String
strCDate = "NULL"
if txtCDate.Text <> vbNullString Then
If IsDate(txtCDate) Then'
strCDate = "#" txtCDate & "#"
End If
End If
Dim mySQL as String
mySQL = "INSERT INTO myTABLE (someDumbDate) VALUES (" & strCDate & ")"
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
May 15th, 2007, 02:34 PM
#6
Thread Starter
Hyperactive Member
Re: NULL dates are driving me nuts
Thanks Gary;
Your suggestion is what I'll most likely do.
Thanks
-
May 16th, 2007, 02:42 PM
#7
Addicted Member
Re: NULL dates are driving me nuts
Isn't there a possibility to trap the error with a try and catch?
Also, for your information, your query is most probably vulnerable to SQL injection.
-
May 16th, 2007, 02:53 PM
#8
Re: NULL dates are driving me nuts
No the query is not vunrable to injection attack. You are testing for valid date before doing anything with the value if it is not a valid date then the NULL will be stored to the database. Every is yelling about the injection attacks if you validate data properly and check for single qoutes inside text strings that will not be an extremly likely attack. And yes I know that all the experts here say the only way to perform SQL inserts is to use parameterized queries only.
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
May 16th, 2007, 03:28 PM
#9
Addicted Member
Re: NULL dates are driving me nuts
Using a value directly from the textbox
mySQL = "INSERT INTO myTABLE (someDumbDate) VALUES (#" & aTextBox.text & "#)"
sound to me like a real issue.
-
May 16th, 2007, 05:00 PM
#10
Re: NULL dates are driving me nuts
It would be if there weren't checks before it.. in this case (where the value must be a valid date, otherwise it is replaced by Null), it is perfectly safe.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|