Question

  • Creator
    Topic
  • #2200696

    explain the code

    Locked

    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
    cnn.Close
    doc.FormFields(“txtCompanyName”).TextInput.Clear
    doc.FormFields(“txtPhone”).TextInput.Clear
    doc.FormFields(“Text1”).TextInput.Clear
    doc.FormFields(“txtname2”).TextInput.Clear
    doc.FormFields(“txtphone2”).TextInput.Clear
    End If
    Set doc = Nothing
    Set cnn = Nothing
    Exit Sub
    ErrHandler:
    MsgBox Err.Number & “: ” & Err.Description, vbOKOnly, “Error”
    On Error GoTo 0
    On Error Resume Next
    Set doc = Nothing
    Set cnn = Nothing

    End Sub

All Answers

  • Author
    Replies
    • #3001099

      Clarifications

      by shayan1986 ·

      In reply to explain the code

      Clarifications

    • #3001096

      Add a check box and option button

      by peconet tietokoneet ·

      In reply to explain the code

    • #3001082

      Hmmm

      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.

Viewing 2 reply threads