Office中国论坛/Access中国论坛

标题: 求助:数据表连接字符串出错(已解决) [打印本页]

作者: cocopig    时间: 2012-7-5 17:26
标题: 求助:数据表连接字符串出错(已解决)
本帖最后由 cocopig 于 2012-7-6 09:41 编辑

ACCESS设有窗体,有“年号”、“编号”和“后续编号”等文本框,通过保存按钮把“年号”、“编号”文本框中的数据用字符串相加后保存到数据表中“SVNum”字段中,“后续编号”保存到“SVFollowUp”字段中,每次新建记录时首先检查表中有没重复记录。我的代码如下:
(文本框“年号”=txtYear,“编号”=txtSVsn,“后续编号”=txtSVFollowUp)
................
    Dim t1 As String
    txtSVYear.SetFocus
    t1 = txtSVYear.Text

    Dim t2 As String
    txtSVsn.SetFocus
    t2 = txtSVsn.Text

    Dim t3 As String
    txtSVFollowUp.SetFocus
    t3 = txtSVFollowUp.Text

  Dim sqlstr As String        '定义sql连接字符串

    sqlstr = "select SVNum,SVFollowUp from InfoProject where SVNum = t1 + t2"     

    '检查“编号”是否重复
    rs.Open sqlstr, CurrentProject.Connection, adOpenDynamic, adLockOptimistic
    If rs.EOF Then

        MsgBox "“编号”已分配,请重新输入!!"
        Exit Sub

    rs.Close
    'Set rs = Nothing

    Else
   
    rs.Close

    sqlstr = "select SVNum,SVFollowUp,District,ProjectName,ProjectAddress," _
                & "StructureType,ConstructionArea,Cost,OvergruandStoery,UndergruandStoery," _
                & "Status from InfoProject"
               
    rs.Open sqlstr, CurrentProject.Connection, adOpenDynamic, adLockOptimistic
   
    rs.AddNew    '添加数据
.......................

运行程序提示上面斜体字代码中至少有一个参数未定义,我估计可能是连接字符串表达错了,请教各位我应该怎样修改???

作者: koutx    时间: 2012-7-5 18:00
sqlstr = "select SVNum,SVFollowUp from InfoProject where SVNum = t1 + t2"
改为:
sqlstr = "select SVNum, SVFollowUp from InfoProject where SVNum = '" & Me.t1 & Me.t2 & "'"
试试
作者: LeeTien    时间: 2012-7-5 21:07
楼上正解
作者: cocopig    时间: 2012-7-6 09:40
参照2楼的提示改为:me.txtYear和me.txtSVsn后测试成功,谢谢帮助!!!




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3