用VB添加数据到SQL数据库的问题

jacken0923 2006-04-11 01:02:21
Private Sub Command1_Click()
If text1.text<>"" and text2.text<>"" then
Adodc1.Recordset.Addnew
Adodc1.Recordset!patientID = Label5.Caption
Adodc1.Recordset!patientName = Text1.Text
Adodc1.Recordset!patientAge = Text2.Text
Adodc1.Recordset!patientSex = Combo2.Text
Adodc1.Recordset!registerOffice = Combo1.Text
Adodc1.Recordset!registerKind = Combo3.Text
Adodc1.Recordset!registerData = Format(Now, "yyyy年m月dd日")
Adodc1.Recordset.Update
Adodc1.Refresh
End If
End Sub
我想按 Command1后给数据库的一张表添加信息,但是上面的代码应该怎么改呢?帮忙看下,3Q!
...全文
359 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
jacken0923 2006-04-12
  • 打赏
  • 举报
回复
问题解决了,原来我把addnew放错位置了,应该是放在formload那才对的,要不然添加一条新行的时候,会把原来的覆盖了。
OK!结帐!
jacken0923 2006-04-11
  • 打赏
  • 举报
回复
问题解决了部分了
公共模块:
Public cn As ADODB.Connection
Public Flag As Boolean

Public Sub GetConnected()
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=医院;Data Source=(local)"
cn.CommandTimeout = 10
cn.Open

End Sub
form的代码:
If text1.text<>"" and text2.text<>"" then
Adodc1.Refresh
Adodc1.Recordset.AddNew
GetConnected
..............
信息是能添加进去了,但是在按了确定键后,text1,Combo1,Combo3所输入的信息就被删除了,导致在表里它们3个相对应的值就变成了空值。也没有什么事件对它们的属性进行修改的。
------------------------------------------------------------------------------------至于patientID是我在VB里设置的一个ID来的。每打开一次该窗口,就会自动加1。
faysky2 2006-04-11
  • 打赏
  • 举报
回复
至于这条,还是不行,它会提示”对象变量或with块变量未设置“。
cn.execute "insert into tablename(...) values(...)"
---------------------------
是不是没连接?

GetConnected '先连接
cn.execute "insert into tablename(...) values(...)"


jackcaixia 2006-04-11
  • 打赏
  • 举报
回复
还有 Adodc1.Recordset.Addnew这句之前最好加一句 Adodc1.Refresh刷新一下数据库
jackcaixia 2006-04-11
  • 打赏
  • 举报
回复
patientID你这个的数据类型是不是INT?如果是的话它的标识你是不是选了"是",如果是的话就肯定出错的~~因为那是自动编号,数据库自动填的~.
如果不是的话你试试这样写
If text1.text<>"" and text2.text<>"" then
Adodc1.Recordset.Addnew
Adodc1.Recordset("patientID") = Label5.Caption
Adodc1.Recordset("patientName") = Text1.Text
Adodc1.Recordset("patientAge") = Text2.Text
Adodc1.Recordset("patientSex") = Combo2.Text
Adodc1.Recordset("registerOffice") = Combo1.Text
Adodc1.Recordset("registerKind") = Combo3.Text
Adodc1.Recordset("registerData") = Format(Now, "yyyy年m月dd日")
Adodc1.Recordset.Update
Adodc1.Refresh
End If
End Sub

jacken0923 2006-04-11
  • 打赏
  • 举报
回复
就是添加不了信息,但没有提示错误,只能把表里的第一条信息给修改了。但当表里没有任何信息的时候,就会提示“BOF或EOF中有一个是“真”,或者当前的记录已被删除,所需的操作要求一个当前的记录。”
Adodc控件已经设置好了ConnectionString和RecordSource属性了。
------------------------------------------------------------------------------------
至于这条,还是不行,它会提示”对象变量或with块变量未设置“。
cn.execute "insert into tablename(...) values(...)"

but thank you all the same!

faysky2 2006-04-11
  • 打赏
  • 举报
回复
但我想实现的功能:把信息添加到表patientID.实现不了。只能将表里的一条信息修改掉。
----------------------------
是有什么错误吗,提示的错误是什么?是否给Adodc控件设置好了ConnectionString和RecordSource属性?
饮水需思源 2006-04-11
  • 打赏
  • 举报
回复
这样试试:
'增加记录:
cn.execute "insert into tablename(...) values(...)"
'修改记录:
cn.execute "update tablename set ... where ..."
'删除记录:
cn.execute "delete from tablename where ..."
jacken0923 2006-04-11
  • 打赏
  • 举报
回复
不好意思,刚才米有说清楚。
公共模块部分的:
Public cn As ADODB.Connection
Public Sub GetConnected()
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=医院;Data Source=(local)"
cn.CommandTimeout = 10
cn.Open
End Sub

form设计的事件:
Private Sub Command1_Click()
If text1.text<>"" and text2.text<>"" then
Adodc1.Recordset.Addnew
Adodc1.Recordset!patientID = Label5.Caption
Adodc1.Recordset!patientName = Text1.Text
Adodc1.Recordset!patientAge = Text2.Text
Adodc1.Recordset!patientSex = Combo2.Text
Adodc1.Recordset!registerOffice = Combo1.Text
Adodc1.Recordset!registerKind = Combo3.Text
Adodc1.Recordset!registerData = Format(Now, "yyyy年m月dd日")
Adodc1.Recordset.Update
Adodc1.Refresh
End If
End Sub
这段代码已经是我对应我的表写的了,但我想实现的功能:把信息添加到表patientID.实现不了。只能将表里的一条信息修改掉。如果是空表,就会说“BOF或EOF中有一个是“真”,或者当前的记录已被删除,所需的操作要求一个当前的记录。”
faysky2 2006-04-11
  • 打赏
  • 举报
回复
Private Sub Form_Load()
'连接sql数据库,具体的自己改过来
Adodc1.ConnectionString = "Provider=MSDataShape;Data Provider=SQLOLEDB.1;Password=;Persist Security Info=False;User ID=sa;Initial Catalog=Test;Data Source=ljx"
Adodc1.CommandType = adCmdTable
Adodc1.RecordSource = "table1" '连接表table1
Adodc1.Refresh
End Sub

Private Sub Command1_Click()
'下边的这些字段什么的,改为自己需要的就行
If text1.text<>"" and text2.text<>"" then
Adodc1.Recordset.Addnew
Adodc1.Recordset!patientID = Label5.Caption
Adodc1.Recordset!patientName = Text1.Text
Adodc1.Recordset!patientAge = Text2.Text
Adodc1.Recordset!patientSex = Combo2.Text
Adodc1.Recordset!registerOffice = Combo1.Text
Adodc1.Recordset!registerKind = Combo3.Text
Adodc1.Recordset!registerData = Format(Now, "yyyy年m月dd日")
Adodc1.Recordset.Update
Adodc1.Refresh
End If
End Sub
faysky2 2006-04-11
  • 打赏
  • 举报
回复
Adodc1.Recordset.Addnew '添加一条空记录
Adodc1.Recordset!patientID = Label5.Caption ' 给 patientID 字段赋值
Adodc1.Recordset!patientName = Text1.Text ' '给 patientName 字段赋值
......
Adodc1.Recordset.Update ' 更新数据到表中
Adodc1.Refresh '刷新Adodc

上面做法的前提是先设置好Adodc的ConnectionString和RecordSource属性

1,217

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧