首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

哪位高手能帮小弟我解决这个VB数据库编程有关问题

2012-03-02 
谁能帮我解决这个VB数据库编程问题!我点击“添加”后,不会DataGrid中不会马上出来添加的信息,要再重新装载后

谁能帮我解决这个VB数据库编程问题!
我点击“添加”后,不会DataGrid中不会马上出来添加的信息,要再重新装载后,才会出现,(程序中没用出现报错)

Public cn As ADODB.Connection
Public rs As ADODB.Recordset

Private Sub Form_Load()

Dim SQL As String
  SQL = "select [学员记录].[编号],[学员记录].[学号],[学员记录].[姓名],[学员记录].[性别],[学员记录].[年龄],[学员记录].[政治面貌],[系记录].[系号],[系记录].[系别]," & _
  "[学员记录].[班级],[学员记录].[状态],[学员记录].[入学时间],[学员记录].[学制时间],[学员记录].[身份证号],[学员记录].[民族],[学员记录].[出生日期],[学员记录].[电子邮箱]," & _
  "[学员记录].[联系电话],[学员记录].[籍贯],[学员记录].[备注] from [学员记录],[系记录]" & _
  " where [学员记录].[系号]=[系记录].[系号] order by [学号]"
  Set rs = New ADODB.Recordset
  rs.Open SQL, cn, adOpenStatic, adLockOptimistic
  Set dtGStuInf.DataSource = rs
  dtGStuInf.Columns("编号").Visible = False
  dtGStuInf.Refresh
  If Not (rs.BOF And rs.EOF) Then
  rs.MoveFirst
  Showing
  End If
End sub

Private Sub cmdAdd_Click()
Dim SQL As String
  SQL = "insert into [学员记录]([学号],[姓名],[性别],[年龄],[政治面貌],[系号],[班级],[状态],[入学时间],[学制时间],[身份证号],[民族],[出生日期],[电子邮箱]," & _
  "[联系电话],[籍贯],[备注]) values('" & txtNub.Text & "','" & txtName.Text & "','" & cboSex.Text & "'," & Val(txtAge.Text) & ",'" & cboFaction.Text & _
  "','" & cboDepartm.Text & "','" & txtClass.Text & "','" & cboStatus.Text & "','" & dtpEnter.Value & "'," & Val(txtSchsyst.Text) & ",'" & txtId.Text & "','" & _
  cboPeople.Text & "','" & dtpBirth.Value & "','" & txtEmail.Text & "','" & txtPhone.Text & "','" & txtHometown.Text & "','" & txtRemark.Text & "') ;"
   
  cn.Execute SQL
  cmdAdd.Caption = "添加"
  Form_Load
  End If

[解决办法]
执行cn.Execute SQL后,要重新打开rs
不用重新装载。

[解决办法]
新建一个sub做显示的工作,Form_Load()和cmdAdd_Click()调用它


Public cn As ADODB.Connection
Public rs As ADODB.Recordset

Private Sub Form_Load()

Call sub_Show

End Sub

Private Sub sub_Show()
Dim SQL As String

SQL = "select [学员记录].[编号],[学员记录].[学号],[学员记录].[姓名],[学员记录].[性别],[学员记录].[年龄],[学员记录].[政治面貌],[系记录].[系号],[系记录].[系别]," & _
"[学员记录].[班级],[学员记录].[状态],[学员记录].[入学时间],[学员记录].[学制时间],[学员记录].[身份证号],[学员记录].[民族],[学员记录].[出生日期],[学员记录].[电子邮箱]," & _
"[学员记录].[联系电话],[学员记录].[籍贯],[学员记录].[备注] from [学员记录],[系记录]" & _
" where [学员记录].[系号]=[系记录].[系号] order by [学号]"

Set rs = New ADODB.Recordset
rs.Open SQL, cn, adOpenStatic, adLockOptimistic
Set dtGStuInf.DataSource = rs
dtGStuInf.Columns("编号").Visible = False
dtGStuInf.Refresh

If Not (rs.BOF And rs.EOF) Then
rs.MoveFirst
Showing
End If

End Sub

Private Sub cmdAdd_Click()
Dim SQL As String

SQL = "insert into [学员记录]([学号],[姓名],[性别],[年龄],[政治面貌],[系号],[班级],[状态],[入学时间],[学制时间],[身份证号],[民族],[出生日期],[电子邮箱]," & _
"[联系电话],[籍贯],[备注]) values('" & txtNub.Text & "','" & txtName.Text & "','" & cboSex.Text & "'," & Val(txtAge.Text) & ",'" & cboFaction.Text & _
"','" & cboDepartm.Text & "','" & txtClass.Text & "','" & cboStatus.Text & "','" & dtpEnter.Value & "'," & Val(txtSchsyst.Text) & ",'" & txtId.Text & "','" & _


cboPeople.Text & "','" & dtpBirth.Value & "','" & txtEmail.Text & "','" & txtPhone.Text & "','" & txtHometown.Text & "','" & txtRemark.Text & "') ;"

cn.Execute SQL
cmdAdd.Caption = "添加"

Call sub_Show

End Sub

热点排行