SQL Injection on New Record Form (1 Viewer)

camerontaylor

New member
Local time
Today, 15:45
Joined
May 11, 2021
Messages
29
I've got a form which a user can enter a new record's info on. I'm wondering if it is possible for SQL Injection to occur with the user input on this form. The new record in the datasheet is also updated to a subform on my main form. If it is possible, then can someone suggest a way for me to sanitize the user input on this form to remove or reduce the possibility of SQL injection. Thanks!
 

theDBguy

I’m here to help
Staff member
Local time
Today, 15:45
Joined
Oct 29, 2018
Messages
21,453
Hi. I believe SQL injection only occurs if you're executing a SQL statement with the input. Are you doing that? If so, can you please show it to us?
 

camerontaylor

New member
Local time
Today, 15:45
Joined
May 11, 2021
Messages
29
Hi. I believe SQL injection only occurs if you're executing a SQL statement with the input. Are you doing that? If so, can you please show it to us?
I don't think so. It's purely just a form to create a new record. I'm not entirely sure of the inner workings behind it. I've attached an image. The save button closes the form, and runs a couple of queries using one of the date fields to create a string of characters and numbers.
 

Attachments

  • dbsc.png
    dbsc.png
    9.4 KB · Views: 182

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:45
Joined
Feb 19, 2002
Messages
43,215
The reason that SQL injection works is because SQL Server et al allow multiple action queries in a single string. Access SQL does not.

You can prove it to yourself. If you put an SQL string in a text control, the SQL string will end up as the column value in your table.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 15:45
Joined
Oct 29, 2018
Messages
21,453
I don't think so.
You don't think SQL injection only happens when executing SQL statements, or you don't think you're executing SQL statements in your form?

What's behind the button "Save Project Details?" Is there any code behind it? If so, can you show it to us?

If you think you have a very simple form, what made you think about SQL injection that made you become concerned with it?
 

Minty

AWF VIP
Local time
Today, 23:45
Joined
Jul 26, 2013
Messages
10,366
The short answer is no. Not on a bound form.
Not without some very convoluted code behind the scenes, which it doesn't sound as if you have.
 

camerontaylor

New member
Local time
Today, 15:45
Joined
May 11, 2021
Messages
29
You don't think SQL injection only happens when executing SQL statements, or you don't think you're executing SQL statements in your form?

What's behind the button "Save Project Details?" Is there any code behind it? If so, can you show it to us?

If you think you have a very simple form, what made you think about SQL injection that made you become concerned with it?
Sorry, I was a bit ambiguous there. I don't think that I'm executing SQL statements in the form.

The Save Button has the following code behind it:
Code:
Private Sub saveProjectDetails_Click()

DoCmd.Close acForm, Me.Name, acSaveNo

incrementMonthNumAndCreateEP

End Sub

Private Sub incrementMonthNumAndCreateEP()
Dim strtDt As Date, EPNum$
Dim SQLcomm1$, SQLcomm2$
Dim monthNum As Integer, r As Recordset

monthNum = DCount("*", "formatDate", "[YM] = '" & Format(Nz(DLookup("StartDate", "ProjectList", DMax("ProjectID", "ProjectList") & "=[ProjectID]")), "yyyymm") & "'")

SQLcomm1 = "UPDATE ProjectList SET MonthCount = " & monthNum & " WHERE ProjectID =" & DMax("ProjectID", "ProjectList") & ";"

DoCmd.SetWarnings (False)
DoCmd.RunSQL SQLcomm1

strtDt = Nz(DLookup("StartDate", "ProjectList", DMax("ProjectID", "ProjectList") & "=[ProjectID]"), 0)

If (strtDt) = 0 Then
    EPNum = ""
Else
    EPNum = "EP" & Format(Nz(DLookup("StartDate", "ProjectList", DMax("ProjectID", "ProjectList") & "=[ProjectID]"), 0), "yymm") & "-" & Format(monthNum, "00")
End If

If EPNum = DLookup("EPNumber", "ProjectList") Then
    EPNum = "EP" & Format(Nz(DLookup("StartDate", "ProjectList", DMax("ProjectID", "ProjectList") & "=[ProjectID]"), 0), "yymm") & "-" & Format(monthNum + 1, "00")
End If

SQLcomm2 = "UPDATE ProjectList SET EPNumber ='" & EPNum & "'WHERE ProjectID = " & DMax("ProjectID", "ProjectList") & ";"

DoCmd.RunSQL SQLcomm2
DoCmd.SetWarnings (True)

End Sub

And the formatDate query looks like this: SELECT ProjectList.StartDate, Format(StartDate,"yyyymm") AS YM FROM ProjectList;

I am purely thinking about protecting against SQL injection because my supervisor has asked that it be implemented where necessary, and so I wanted to get confirmation of whether I need to be sanitizing or not.
 

camerontaylor

New member
Local time
Today, 15:45
Joined
May 11, 2021
Messages
29
The reason that SQL injection works is because SQL Server et al allow multiple action queries in a single string. Access SQL does not.

You can prove it to yourself. If you put an SQL string in a text control, the SQL string will end up as the column value in your table.
So in this particular case, I don't have to worry about it?
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:45
Joined
Sep 21, 2011
Messages
14,231
You might want to start Dimming your variables correctly as well.?
Each has to specified for Type?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 15:45
Joined
Oct 29, 2018
Messages
21,453
Ok, perfect. Thanks for the confirmation
You're welcome. However, I'm still curious. What made you think of SQL injection in the first place? Did you read about it somewhere? Where? Thanks.
 
Last edited:

Users who are viewing this thread

Top Bottom