VB Access 更新数据类型不匹配 [英] VB Access Update data type mismatch

查看:97
本文介绍了VB Access 更新数据类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,我是 vb 新手,

Sorry in advance, Im new to vb,

我正在创建一个小型应用程序作为 MCQ 测验进行分配,我使用相同的形式,但使用 [nextques] 的功能继续下一个问题并使用 [answer] 更新用户对数据库的回答(访问).

Im creating a small application for assignment as a MCQ quiz, Im using the same form but use the function of [nextques] proceed to next question and use [answer] to update the user answer to database(access).

我以前使用 SQL Server,它运行良好,但由于我需要在学校的计算机上运行该应用程序,他们不允许我安装任何东西,我完全不知道然后决定使用访问,因为它更容易导出并将其包含在调试文件中.

I previously uses SQL Server and it work good but due to i need to run the application in school's computer where they wont allow me to install anything, I have no idea at all then decided to use access as it is easier to export and include it in debug file.

我已经浏览了一些主题并进行了一些更改,但问题仍然存在.我将访问中的 [input] 设置为整数,我确实在其上放置了参数,但它仍然无法正常工作.

Ive go through some topics as well as did some changes however the problem persist. I set the [input] in access as integer, i did put parameter on it but it still not working.

错误代码:条件表达式中的数据类型不匹配

ERROR code : Data type mismatch in criteria expression

Public Function answer(ans As Integer)
    Try
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mom.mdb;"
        con.Open()
        cmd.Connection = con
        ' Update the answer 1234 (as abcd) according to id in lblnum.text
        Dim updatecmd As String = "UPDATE question SET [input] = @ans WHERE id = '" & lblnum.Text & "'"

        cmd.Parameters.AddWithValue("@ans", ans)
        cmd.CommandText = updatecmd
        updatecmd = cmd.ExecuteNonQuery()
        con.Close()

        'Proceed to next question by calling Function NextQues
        nextques()

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    Return 0
End Function

我也尝试这样做,但是另一个错误显示:无法使用与其底层 RCW 分离的 COM 对象

I tried also to make it like this however another error shows: COM object that has been separated from its underlying RCW cannot be used

Public Function answer(ans As Integer)
    Dim countid As Integer = 1
    Try
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mom.mdb;"
        con.Open()
        cmd.Connection = con

        Dim updatecmd As String = "UPDATE question SET [input] = @ans WHERE id = @countid"

        cmd.Parameters.AddWithValue("@countid", countid)
        cmd.Parameters.AddWithValue("@ans", ans)
        cmd.CommandText = updatecmd
        updatecmd = cmd.ExecuteNonQuery()

        countid = countid + 1
        con.Close()



        'Proceed to next question by calling Function NextQues
        nextques()

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    Return 0
End Function

推荐答案

更改此行

Dim updatecmd As String = "UPDATE question SET [input] = @ans WHERE id = '" & lblnum.Text & "'"

Dim updatecmd As String = "UPDATE question SET [input] = @ans WHERE id = " & lblnum.Text

这篇关于VB Access 更新数据类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆