[VB6 ] 資料庫寫入

看板Visual_Basic作者 (即將再當澳客)時間10年前 (2013/11/22 14:57), 編輯推噓4(407)
留言11則, 4人參與, 最新討論串1/1
請問黃色和綠色部份一樣 日期有記進資料庫,但車號卻沒記進去,是哪裡有問題嗎? ** 界面簡介: 車號combo 三個記錄日期的text(年/月/日) 一個公升text 一個價格text 一里程數text 四個cammend 一個adodc及grid ** 所有的text都成功的記錄進資料庫裡,但只有combo的記錄失敗 剛稍微修改了一些內容 把原本以text用DataSource及DataField來跟資料庫連結 修改成 全部以Adodc.Record.Field(i) = val(text.text)的模式寫進access裡 利用 逐行 功能來看,跑到車牌那裡時,確定 rs.Fields(0) 也有寫進車牌號碼了 但是Record.update之後, 每一個都成功寫進資料庫了,唯獨車號寫不進去 ** 再次測試,跳出msgbox確認是否記錄時,車號有出現在DataGrid裡 但是按下"確定"按鈕後,車牌號碼卻消失了… ================================= Private Sub Form_Load() '車輛選單設定 truck(0) = "XS-001" truck(1) = "XS-002" truck(2) = "XS-003" truck(3) = "XS-004" truck(4) = "XS-005" cboTruckPlate.Clear For i = 0 To 4 cboTruckPlate.AddItem (truck(i)) Next cboTruckPlate.ListIndex = 0 '按鈕名稱設定 cmdAddNew.Caption = "新增" cmdSave.Caption = "儲存" cmdDelete.Caption = "刪除" cmdFinish.Caption = "結束" '鎖定輸入格 InputLocked *** Private Sub cmdSave_Click() '輸入的資料全集合在字串裡 '車號必填 temp = "" If cboTruckPlate.ListIndex = -1 Then return_msg = MsgBox("車號忘了選", 32, "請選擇車號") cboTruckPlate.SetFocus Exit Sub Else '記錄車號 tPlateRecord = truck(cboTruckPlate.ListIndex) AdoDiesel.Recordset.Fields(0) = tPlateRecord temp = "車號:" & txtTruckPlate.Text & vbCrLf End If '日期必填 If txtYear.Text <> "" And txtMonth.Text <> "" And txtDay.Text <> "" Then '記錄日期 tDate = Format(Str(txtYear.Text), "0000") + "/" + Format(Str(txtMonth.Text), "00") + "/" + Format(Str(txtDay.Text), "00") AdoDiesel.Recordset.Fields(1) = tDate temp = temp & "日期:" & Str(txtYear.Text) & "年" & Str(txtMonth.Text) & " 月" & Str(txtDay.Text) & "日" & vbCrLf Else return_msg = MsgBox("日期沒輸入", 32, "請輸入日期") txtYear.SetFocus Exit Sub End If '加油量必填 If txtLiter.Text = "" Then return_msg = MsgBox("今天加了幾公升?", 32, "請輸入加油量") txtLiter.SetFocus Exit Sub Else AdoDiesel.Recordset.Fields(2) = Val(txtLiter.Text) temp = temp & "公升:" & Str(txtLiter.Text) & vbCrLf End If If txtPrice.Text <> "" Then AdoDiesel.Recordset.Fields(3) = Val(txtPrice.Text) temp = temp & "價格:" & Str(txtPrice.Text) & vbCrLf End If If txtMileage.Text <> "" Then AdoDiesel.Recordset.Fields(4) = Val(txtMileage.Text) temp = temp & "里程數:" & Str(txtMileage.Text) End If If temp <> "" Then return_value = MsgBox(temp, 33, "以下資料是否正確") '檢測資料是否正確 If return_value = 1 Then '資料正確即存入 AdoDiesel.Recordset.Update AdoDiesel.Recordset.Update AdoDiesel.Refresh Else return_msg = MsgBox("放棄儲存,請重新輸入", 48, "資料未存入") AdoDiesel.Refresh End If End If AdoDiesel.Recordset.Update '再重整並移動到最後 AdoDiesel.Refresh AdoDiesel.Recordset.MoveLast '輸入結束,輸入格鎖定 Form_Load SetFilesWidth cmdAddNew.SetFocus AdoDiesel.Recordset.MoveLast End Sub -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 1.124.158.162

11/22 17:56, , 1F
看不出是何種資料庫
11/22 17:56, 1F

11/22 17:58, , 2F
索引<>值
11/22 17:58, 2F
簡單的把Text裡的值放進access裡,只有第一個是用combo選擇內定的車牌號碼 ※ 編輯: chrisjon 來自: 1.124.158.162 (11/22 19:33) ※ 編輯: chrisjon 來自: 1.124.158.162 (11/22 19:39) ※ 編輯: chrisjon 來自: 1.142.154.153 (11/24 16:54)

11/24 23:04, , 3F
你 FormLoad 中的 truck 變數範圍是在哪邊? 全域嗎?
11/24 23:04, 3F

11/24 23:04, , 4F
truck(cboTruckPlate.ListIndex) 這邊怎麼不直接抓Combo值
11/24 23:04, 4F

11/25 01:50, , 5F
另外設一個模組,在那裡設定Public truck(4) As String*6
11/25 01:50, 5F

11/25 01:58, , 6F
不好意思,不是很懂抓combo值
11/25 01:58, 6F

11/25 05:37, , 7F
請確認資料庫是不是有值,只是不是自己想的值
11/25 05:37, 7F

11/25 11:55, , 8F
你輸入的車號是否滿足資料庫車號的欄位?
11/25 11:55, 8F
找到可能問題了…… 手動輸入車號並沒有問題,是可以儲存的,但是利用程式寫卻一直都寫不進去 剛沒事點點DataGrid的車號空白欄位 因為我設定該欄位只能有6碼,發現格子裡都被空格佔滿 到底……空格是在哪裡被佔滿的…~.~? ※ 編輯: chrisjon 來自: 1.143.42.146 (11/26 05:13)

11/26 12:41, , 9F
索引
11/26 12:41, 9F

11/26 13:15, , 10F
不是很明白,請問可以簡單解說一下嗎? 謝謝
11/26 13:15, 10F

11/26 17:44, , 11F
不要先使用陣列,先直接填字串
11/26 17:44, 11F
文章代碼(AID): #1IZm12Iv (Visual_Basic)