Quantcast
Channel: VBForums - Database Development
Viewing all articles
Browse latest Browse all 2918

Data Type Mismatch Vb.net Access

$
0
0
I am trying to insert data into my Access database using vb for an application I am building. I have been BEATING my had off of an invisible wall trying to get the data to enter into the database. Below is the code, below that I will explain it more.

Code:

Private Sub btnSubmitExpense_Click(sender As Object, e As EventArgs) Handles btnSubmitExpense.Click
    provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
    dataFile = "C:\PFADatabase.accdb"
    connString = provider & dataFile
    myConnection.ConnectionString = connString
    myConnection.Open()
    Dim str As String
    str = "Insert into Expenses ([ExpenseTypeID], [DateOfExpense],[AmountOfExpense]) Values(?,?,?)"
    Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
    cmd.Parameters.Add(New OleDbParameter("ExpenseTypeID", CType(cboCategoryOfExpense.Text, String)))
    cmd.Parameters.Add(New OleDbParameter("DateOfExpense", CType(dateOfExpense.Text, String)))
    cmd.Parameters.Add(New OleDbParameter("AmountOfExpense", CType(txtAmountOfExpense.Text, String)))

    Try
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        myConnection.Close()
        'clear the form below
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

[ExpenseTypeID] is actually a lookup. The object is a combobox in vb that I have programmed above the code I posted here. [DateOfExpense] is also a Date object in VB. I just dragged it over. I want that to be associated with everything in that table. [AmountOfExpense] is again, tied into the db.

Basically a user enters the date using the tool, then chooses a category of their expense and then enters the amount. This is all one table but the expenseType is a Lookup actually within Access to another table so we could have the dropdown. Everything works well until I click the save button. Then whenever i do that I get the exception the type is mismatched. How do I fix that? I'm GUESSING it is the text string being wrong, but I've tried other things so I am thinking it is a bit more than that which is why I came here to you professionals to get input.

Thank you.

The error is coming in at
Code:

cmd.ExecuteNonQuery()
and says "Data type mismatch in criteria expression. "

Edit2: I went and looked at her Db. She has the amount one set to currency. The EXpenseID set to autonumber, and DateAndTime set to date/time in Access.

Viewing all articles
Browse latest Browse all 2918

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>