任何人都可以请求帮助解决此错误的方法吗?我得到一个错误“错误:查询表达式'''ans'中的语法错误(缺少运算符)。”这是我的代码:
Imports System.Data
Imports System.Data.OleDb
Public Class frmExam : Inherits System.Windows.Forms.Form
Dim ds As New DataSet()
Dim qno() As Integer
Dim pos As Integer
Private Sub btnFinish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFinish.Click
ProcessAnswer()
Dim i, marks As Integer
Dim dr As DataRow
marks = 0
For i = 0 To NOQ - 1
dr = ds.Tables(0).Rows(qno(i))
If Not IsDBNull(dr.Item("ans")) AndAlso dr.Item("CorrectAns") = dr.Item("ans") Then
marks += 1
End If
Next
Try
con.Open()
Dim cmd As New OleDbCommand("insert into stud values(stud.nextval,'" & SubjectCode & "','" & Username & "','" & marks & ")", con)
MsgBox(cmd.CommandText)
cmd.ExecuteNonQuery()
Dim msg As String
msg = "UserName : " & Username & ControlChars.CrLf & _
"Subject : " & SubjectCode & ControlChars.CrLf & _
"Total Questions : " & NOQ & ControlChars.CrLf & _
"Marks : " & marks
MsgBox(msg, "Result")
Catch ex As Exception
MsgBox(ex.Message)
Finally
If con.State <> ConnectionState.Closed Then
con.Close()
End If
End Try
Me.Dispose()
End Sub
Private Sub frmExam_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ReDim qno(NOQ)
Try
Dim da As New OleDbDataAdapter("select Question, Ans1, Ans2, Ans3, Ans4, CorrectAns, QType, '' ans from question where SubjectCode = " & SubjectCode, con)
da.Fill(ds, "question")
Catch ex As Exception
MsgBox("Error:" & ex.Message)
Me.Dispose()
End Try
Randomize()
Dim totrows As Integer
totrows = ds.Tables(0).Rows.Count
Dim i, r, j As Integer
Dim present As Boolean
i = 0
Do While i < 5
r = CInt((totrows - 1) * Rnd())
present = False
For j = 0 To i
If r = qno(j) Then
present = True
Exit For
End If
Next
If Not present Then
qno(i) = r
i = i + 1
End If
Loop
pos = 0
DisplayQuestion()
End Sub
Sub DisplayQuestion()
Dim row As DataRow
lblQno.Text = Str(pos + 1) & "/" & NOQ
lblSubcode.Text = "Subject : " & SubjectCode
row = ds.Tables(0).Rows(qno(pos))
txtQuestion.Text = row.Item(0)
txtAns1.Text = row.Item(1)
txtAns2.Text = row.Item(2)
txtAns3.Text = row.Item(3)
txtAns4.Text = row.Item(4)
lblQNo2.Text = Str(pos + 1) & "/" & NOQ
lblSubCode2.Text = "Subject:" & SubjectCode
row = ds.Tables(0).Rows(qno(pos))
txtQuestion2.Text = row.Item(0)
txtAnsTrue.Text = row.Item(1)
txtAnsFalse.Text = row.Item(2)
lblQNo3.Text = Str(pos + 1) & "/" & NOQ
lblSubcode.Text = "Subject : " & SubjectCode
row = ds.Tables(0).Rows(qno(pos))
txtQuestion3.Text = row.Item(1)
txtAns.Text = row.Item(2)
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
ProcessAnswer()
If pos < NOQ - 1 Then
pos = pos + 1
DisplayQuestion()
Else
Beep()
End If
End Sub
Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
ProcessAnswer()
If pos > 0 Then
pos = pos - 1
DisplayQuestion()
End If
End Sub
Public Sub ProcessAnswer()
Dim row As DataRow
Dim ans As String = ""
row = ds.Tables(0).Rows(qno(pos))
If rdbtnAns1.Checked Then
ans = "1"
End If
If rdbtnAns2.Checked Then
ans = "2"
End If
If rdbtnAns3.Checked Then
ans = "3"
End If
If rdbtnAns4.Checked Then
ans = "4"
End If
If rdbtnTrue.Checked Then
ans = "1"
End If
If rdbtnFalse.Checked Then
ans = "2"
End If
If txtAns.Text = "" Then
ans = "txtAns"
End If
ds.Tables(0).Rows(qno(pos)).Item("Ans1") = ans
End Sub
Private Sub btnPrev2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev2.Click
ProcessAnswer()
If pos > 0 Then
pos = pos - 1
DisplayQuestion()
End If
End Sub
Private Sub btnNext2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext2.Click
ProcessAnswer()
If pos < NOQ - 1 Then
pos = pos + 1
DisplayQuestion()
Else
Beep()
End If
End Sub
Private Sub btnPrev3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev3.Click
ProcessAnswer()
If pos > 0 Then
pos = pos - 1
DisplayQuestion()
End If
End Sub
Private Sub btnNext3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext3.Click
ProcessAnswer()
If pos < NOQ - 1 Then
pos = pos + 1
DisplayQuestion()
Else
Beep()
End If
End Sub
End Class
这是突出显示的texxt:totrows = ds.Tables(0).Rows.Count
答案 0 :(得分:1)
如果要为字段表达式使用别名,Access要求您使用AS
关键字。
这会失败......
select Question, Ans1, Ans2, Ans3, Ans4, CorrectAns, QType, '' ans from question
可以工作......
select Question, Ans1, Ans2, Ans3, Ans4, CorrectAns, QType, '' AS ans from question
但是,对于名称与表名匹配的字段,我会感到不安。如果您无法重命名该字段,请对表进行别名,并使用表别名限定字段。
select q.Question, q.Ans1, q.Ans2, q.Ans3, q.Ans4, q.CorrectAns, q.QType, '' AS ans from question AS q