Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Newb VB.NET/Access Q: INSERT INTO...

  • 01-03-2004 10:37am
    #1
    Registered Users, Registered Users 2 Posts: 912 ✭✭✭


    I am just getting VB.NET and Access talking for the first time and I cannot get this INSERT SQL code to work:
    [PHP]Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

    Dim strDSN As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\myDB.mdb"

    Dim strSQL As String = "INSERT INTO Companies(CompanyName) VALUES('" & txtNewCompany & "')"

    ' create Objects of ADOConnection and ADOCommand
    Dim myConnectionString As New OleDbConnection(strDSN) '
    Dim myQuery As New OleDbCommand(strSQL, myConnectionString)

    Try
    myConnectionString.Open()
    myQuery.ExecuteNonQuery()
    Catch myError As Exception
    MsgBox("Uh oh! Something wrong!")
    Finally
    myConnectionString.Close()
    End Try

    End Sub
    [/PHP]
    This is causing build error
    C:\MyPrograms\ConnectAccess\Form1.vb(153): Operator '&' is not defined for types 'String' and 'System.Windows.Forms.TextBox'.

    I have a text entry in Form1 called txtNewCompany and if I try

    [PHP]Dim strSQL As String = "INSERT INTO Companies(CompanyName) VALUES('txtNewCompany')"
    [PHP]

    the actual string 'txtNewCompany' goes into the table, not the value

    Any help much appreciated


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    I think you have to use the .Text function (not VB.NET savvy, me), otherwise VB thinks you are trying to pass an object reference to the SQL statement...


  • Registered Users, Registered Users 2 Posts: 912 ✭✭✭chakotha


    Thanks that did it!


Advertisement