explain the code

By shayan1986 ·
I need some one hellp me to understand this code ,so I could add an option button and check box

Private Sub CommandButton1_Click()

Dim cnn As ADODB.Connection
Dim strConnection As String
Dim strSQL, strsql2 As String
Dim strPath As String
Dim doc As Word.Document
Dim strCompanyName As String
Dim strPhone As String
Dim strtext1 As String
Dim strname2, strphone2 As String
Dim bytContinue As Byte
Dim lngSuccess, lngSuccess2 As Long
Set doc = ThisDocument
On Error GoTo ErrHandler
strCompanyName = Chr(39) & doc.FormFields("txtCompanyName").Result & Chr(39)
strPhone = Chr(39) & doc.FormFields("txtPhone").Result & Chr(39)
strtext1 = Chr(39) & doc.FormFields("Text1").Result & Chr(39)
strname2 = Chr(39) & doc.FormFields("txtname2").Result & Chr(39)
strphone2 = Chr(39) & doc.FormFields("txtphone2").Result & Chr(39)

bytContinue = MsgBox("Do you want to insert this record?", vbYesNo, "Add Record")
If bytContinue = vbYes Then
strSQL = "INSERT INTO Shippers(CompanyName, Phone,text1) VALUES (" & strCompanyName & "," & strPhone & "," & strtext1 & ")"
strsql2 = "INSERT INTO part2(name2,phone2)VALUES(" & strname2 & "," & strphone2 & ")"

strPath = "D:\nor.mdb"

strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source = " & strPath
Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Properties("Data Source").Value = strPath
.Open strConnection
End With
cnn.Execute strSQL, lngSuccess
cnn.Execute strsql2, lngSuccess2
End If
Set doc = Nothing
Set cnn = Nothing
Exit Sub
MsgBox Err.Number & ": " & Err.Description, vbOKOnly, "Error"
On Error GoTo 0
On Error Resume Next
Set doc = Nothing
Set cnn = Nothing

End Sub

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -


by Tony Hopkinson In reply to explain the code

Presumably this code sits on a form.
Click CommandButton1.
It takes the content of the Controls, builds an insert statement. If you confirm that you want to add. Connects to Nor.MDb, executes the query, clears the entry form so you can do the next.

To add more data to a shipper record.
You add a control to give you the input. A variaable to pick up the value. Then you'd need a column in the shippers table to store it. Then you'd add that column name to teh insert statment, and it's value in Values) and then clear it where the others are cleared.
Option group and checkbox you'll ahve to think about what clear means. E.g is teh default not set (null) or one of the values.
Yes, No) or for option Group OPt1,Opt2...Optn

Have a play.

Oh this has got sql injection attack written all over it, so I presume it's an internal noddy type thing.

Related Discussions

Related Forums