急!!!!!求助!!!VB问题
Private Sub Command1_Click()Dim str As String
Dim i As Integer
sSQL = "select * from goods order by id desc"
If Rs.State <> 0 Then Rs.Close
Set Rs = conn.Execute(sSQL)
If Not Rs.EOF Then
' MsgBox "编号 [ " & Trim(Text1.Text) & " ] 已存在,自动编辑下一个!", vbInformation, "提示窗口"
str = Rs.Fields("id")
i = Val(str)
i = i + 1
str = CStr(i)
sSQL = "insert into goods values('" & str & "','" & Combo1.Text & "','" & Combo2.Text & "','" & Text2.Text & "','" & Text3.Text & "')"
conn.Execute sSQL
' Exit Sub
End If
Unload Me
Form2.Show
MsgBox "数据添加成功"
End Sub
这段程序为什么运行起来,加数据到数据库要点两下按钮才能吧数据加进去,谁能帮我下,谢谢!!!
--------------------编程问答-------------------- 这个东西自己调试下不是很方便的
sSQL = "insert into goods values('" & str & "','" & Combo1.Text & "','" & Combo2.Text & "','" & Text2.Text & "','" & Text3.Text & "')"
conn.Execute sSQL
这里下断点
看看第一次点的时候能运行到不
不能运行到说明是上面代码的问题 --------------------编程问答-------------------- 不是很明白你说的??能详细点说吗 --------------------编程问答-------------------- 我看了下
可能问题在
If Not Rs.EOF Then
你只判断了not Rs.EOF的情况
也就是能找到相同内容的情况
当你新增的时候
肯定就是Rs.EOF
加个else运行Rs.EOF的代码就可以了 --------------------编程问答-------------------- 能把程序给我写下吗??我运行不出来 --------------------编程问答-------------------- 那我就问下你
If Not Rs.EOF Then
这个如果没有会怎么样
我觉得好像没影响啊
你把这个和下面的end if去掉看看 --------------------编程问答-------------------- 去了是没什么影响,但是还是要点两下才能把数据加进去,你有QQ吗?我想加你详细谈谈,谢谢 --------------------编程问答-------------------- 给你几个函数,绝对能用
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'函数功能:连接指定的数据库
'参数说明:cnnP:数据库连接对象;
' :adoP:数据集存储对象;
' :strPath:数据库路径;
' :strPassword:数据库密码;
'返回说明:True:连接成功 False:连接失败
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Public Function funConnectDataBase(cnnP As ADODB.Connection, adoP As ADODB.Recordset, ByVal strPath As _
String, ByVal strPassword As String) As Boolean
On Error GoTo errFunction
Set cnnP = New ADODB.Connection
Set adoP = New ADODB.Recordset
cnnP.Provider = "Microsoft.Jet.OLEDB.4.0"
cnnP.Open "Data Source = " & strPath & ";jet oledb:database password=" & strPassword
funConnectDataBase = True
Exit Function
errFunction:
funConnectDataBase = False
End Function
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'函数功能:关闭数据库连接对象和数据文件的关联
'参数说明:cnnP:数据库连接对象;
' :adoP:数据库存储对象;
'返回说明:True:关闭连接成功 False:关闭连接失败
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Public Function funCloseDataBase(cnnP As ADODB.Connection, adoP As ADODB.Recordset) As Boolean
On Error GoTo errFunction
Set adoP = Nothing
Set cnnP = Nothing
funCloseDataBase = True
Exit Function
errFunction:
funCloseDataBase = False
End Function
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'过程功能:对指定的对象执行指定的SQL语句
'参数说明:cnnP:ADO连接对象
' :adoP:ADO记录集对象
' :strSql:SQL语句
' :bolQueryRecord:是否是查询记录集
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Public Sub subExcuteSQL(cnnP As ADODB.Connection, adoP As ADODB.Recordset, strSql As String, bolQueryRecord _
As Boolean)
If bolQueryRecord Then '如果是查询记录集
adoP.Open strSql, cnnP, adOpenStatic, adLockBatchOptimistic
Else
cnnP.Execute strSql
End If
End Sub
--------------------编程问答-------------------- 有问题这里说吧
还是你那个if的问题
按两次说明第一次没有执行到
你分析下到底第一次为什么没有执行到不就好了
你在 If Rs.State <> 0 Then Rs.Close
这里下个断点,就是在代码前面的地方点一下,变红以后就下好一个断点了。
你看看前后数值这么变化 为什么会只运行一次 --------------------编程问答--------------------
Private Sub Command1_Click()--------------------编程问答-------------------- 不是很明白啊!!!第一次按的编号是加进去了,但是其他数据加不进去。因为编号是自动排序的 --------------------编程问答-------------------- 楼主可以试着把插入代码单独拿出来做测试,如果没问题应该能找到问题了 --------------------编程问答-------------------- 上面那位朋友提供的程序跟我自己的程序运行起来的效果是一样的,都要点两下才能加进去 --------------------编程问答--------------------
Dim str As String
Dim i As Integer
sSQL = "select max(id) as id from goods"
If Rs.State <> 0 Then Rs.Close
rs.open sSQL,conn,adopenkeyset,adlockreadonly
if rs.recordcount=0 then
i=0
else
if not isnull(rs!id) then
i=rs!id
else
i=0
end if
end if
rs.close
str = CStr(i+1)
sSQL = "insert into goods values('" & str & "','" & Combo1.Text & "','" & Combo2.Text & "','" & Text2.Text & "','" & Text3.Text & "')"
conn.Execute sSQL
Unload Me
Form2.Show
MsgBox "数据添加成功"
End Sub
大概不是点两次才加进去,而是点第二次才显示在你的界面里吧?
--------------------编程问答--------------------
有可能
lz返回以后刷新数据库下看看 --------------------编程问答-------------------- 点一下,然后查看数据库,是否有数据 --------------------编程问答--------------------
我看了,现在我加进去的东西是点一下能加了,但是加下一个的时候上一个加的又自动重新加了一次。从上往下加下来编码是1,2,2,3,3, --------------------编程问答-------------------- Private Sub Command1_Click()
Dim str As String
Dim i As Integer
sSQL = "select * from goods order by id desc"
If Rs.State <> 0 Then Rs.Close
Set Rs = conn.Execute(sSQL)
If Not Rs.EOF Then
' MsgBox "编号 [ " & Trim(Text1.Text) & " ] 已存在,自动编辑下一个!", vbInformation, "提示窗口"
str = Rs.Fields("id")
i = Val(str)
i = i + 1
str = CStr(i)
sSQL = "insert into goods("
sSQL = sSQL & "id" & ","
sSQL = sSQL & "factory" & ","
sSQL = sSQL & "model" & ","
sSQL = sSQL & "price" & ","
sSQL = sSQL & "counts" & ")"
sSQL = sSQL & "values('"
sSQL = sSQL & str & "','"
sSQL = sSQL & (Combo1.Text) & "','"
sSQL = sSQL & (Combo2.Text) & "','"
sSQL = sSQL & (Text2.Text) & "','"
sSQL = sSQL & (Text3.Text) & "')"
conn.Execute sSQL
'sSQL = "insert into goods values('" & str & "','" & Combo1.Text & "','" & Combo2.Text & "','" & Text2.Text & "','" & Text3.Text & "')"
conn.Execute sSQL
' Exit Sub
End If
Unload Me
Form2.Show
MsgBox "数据添加成功"
End Sub
这是现在我用的程序,大家帮忙解决下 --------------------编程问答-------------------- conn.Execute sSQL
'sSQL = "insert into goods values('" & str & "','" & Combo1.Text & "','" & Combo2.Text & "','" & Text2.Text & "','" & Text3.Text & "')"
conn.Execute sSQL
楼主执行了两遍当然会点一次插2条一样的记录了啊!~ 去掉一个 --------------------编程问答--------------------
真应了你的名字了
死循环 --------------------编程问答--------------------
郁闷了,去掉一个是插一条了,但是要点两次才插一条 --------------------编程问答-------------------- 编码是按正常顺序下来了,但是编程隔一个编码插一条了,我写完第2条后,要第4条才能写进去了,第3条只显示编码,而编码是自动加的,不需要我写 --------------------编程问答-------------------- 用Leftie的方式 --------------------编程问答--------------------
Private Sub Command1_Click()
Dim str As String
Dim i As Integer
sSQL = "select max(id) as id from goods"
If Rs.State <> 0 Then Rs.Close
Set Rs = conn.Execute(sSQL)
If Rs.RecordCount <= 0 Then
i = 0
Else
If Not IsNull(Rs!id) Then
i = Rs!id
Else
i = 0
End If
End If
Rs.Close
i = i + 1
str = CStr(i)
sSQL = "insert into goods("
sSQL = sSQL & "id" & ","
sSQL = sSQL & "factory" & ","
sSQL = sSQL & "model" & ","
sSQL = sSQL & "price" & ","
sSQL = sSQL & "counts" & ")"
sSQL = sSQL & "values('"
sSQL = sSQL & str & "','"
sSQL = sSQL & (Combo1.Text) & "','"
sSQL = sSQL & (Combo2.Text) & "','"
sSQL = sSQL & (Text2.Text) & "','"
sSQL = sSQL & (Text3.Text) & "')"
conn.Execute sSQL
Unload Me
Form2.Show
MsgBox "数据添加成功"
End Sub
--------------------编程问答-------------------- Private Sub Command1_Click()
Dim str As String
Dim i As Integer
sSQL = "select * from goods order by id desc"
If Rs.State <> 0 Then Rs.Close
Set Rs = conn.Execute(sSQL)
If Not Rs.EOF Then
' MsgBox "编号 [ " & Trim(Text1.Text) & " ] 已存在,自动编辑下一个!", vbInformation, "提示窗口"
str = Rs.Fields("id")
i = Val(str)
i = i + 1
str = str & Format$(i,"000;")
sSQL = "insert into goods("
sSQL = sSQL & "id" & ","
sSQL = sSQL & "factory" & ","
sSQL = sSQL & "model" & ","
sSQL = sSQL & "price" & ","
sSQL = sSQL & "counts" & ")"
sSQL = sSQL & "values('"
sSQL = sSQL & str & "','"
sSQL = sSQL & (Combo1.Text) & "','"
sSQL = sSQL & (Combo2.Text) & "','"
sSQL = sSQL & (Text2.Text) & "','"
sSQL = sSQL & (Text3.Text) & "')"
conn.Execute sSQL
'sSQL = "insert into goods values('" & str & "','" & Combo1.Text & "','" & Combo2.Text & "','" & Text2.Text & "','" & Text3.Text & "')"
conn.Execute sSQL
' Exit Sub
End If
Unload Me
Form2.Show
MsgBox "数据添加成功"
End Sub
--------------------编程问答-------------------- 这段程序也是有问题的啊,编码都不是从1开始的,而且程序运行有错误,麻烦你再发一段吧 --------------------编程问答-------------------- 我的问题有人解决没????急啊!!!! --------------------编程问答-------------------- 应该用Leftie的思路,你的思路没有考虑没有记录的情况,23楼运行怎么样? --------------------编程问答-------------------- Private Sub Command1_Click()
Dim str As String
Dim i As Integer
sSQL = "select max(id) as id from goods"
If Rs.State <> 0 Then Rs.Close
Set Rs = conn.Execute(sSQL)
If Rs.RecordCount <= 0 Then
i = 0
Else
If Not IsNull(Rs!id) Then
i = Rs!id
Else
i = 0
End If
End If
Rs.Close
i = i + 1
str = str & i
sSQL = "insert into goods("
sSQL = sSQL & "id" & ","
sSQL = sSQL & "factory" & ","
sSQL = sSQL & "model" & ","
sSQL = sSQL & "price" & ","
sSQL = sSQL & "counts" & ")"
sSQL = sSQL & "values('"
sSQL = sSQL & str & "','"
sSQL = sSQL & (Combo1.Text) & "','"
sSQL = sSQL & (Combo2.Text) & "','"
sSQL = sSQL & (Text2.Text) & "','"
sSQL = sSQL & (Text3.Text) & "')"
conn.Execute sSQL
Unload Me
Form2.Show
MsgBox "数据添加成功"
End Sub --------------------编程问答-------------------- 23楼的运行完提示i=RS!id有错误,提示窗口是实时错误6,溢出 --------------------编程问答-------------------- 楼主是不是记录太多了,看一下rs!ID值是多少?
把 Dim i As Integer 改成 Dim i As long 试试
--------------------编程问答-------------------- 看来看去好像少个END IF??? --------------------编程问答--------------------
有可能这个原因
Private Sub Command1_Click()--------------------编程问答--------------------
Dim str As String
Dim i As long
sSQL = "select max(id) as id from goods"
If Rs.State <> 0 Then Rs.Close
Set Rs = conn.Execute(sSQL)
If Rs.RecordCount <= 0 Then
i = 0
Else
If Not IsNull(Rs!id) Then
i = Rs!id
Else
i = 0
End If
End If
Rs.Close
i = i + 1
str = CStr(i)
sSQL = "insert into goods("
sSQL = sSQL & "id" & ","
sSQL = sSQL & "factory" & ","
sSQL = sSQL & "model" & ","
sSQL = sSQL & "price" & ","
sSQL = sSQL & "counts" & ")"
sSQL = sSQL & "values('"
sSQL = sSQL & str & "','"
sSQL = sSQL & (Combo1.Text) & "','"
sSQL = sSQL & (Combo2.Text) & "','"
sSQL = sSQL & (Text2.Text) & "','"
sSQL = sSQL & (Text3.Text) & "')"
conn.Execute sSQL
Unload Me
Form2.Show
MsgBox "数据添加成功"
End Sub
不给完整代码,他不知道按谁给的代码改 --------------------编程问答--------------------
补充:VB , 数据库(包含打印,安装,报表)