2010-10-03 63 views
1

我創建了一個程序,添加和返回設備,我的問題是,它是因爲我借用表,我回設備表是在一個表only..representing這些領域更新域通的Visual Basic

「productnumber」

「產品名稱」

「dateborrowed」

「datereturned」

「博rrowername」

‘狀態’

當用戶返回設備,進入上字段相同的數據將使我productnumber我database..especially錯誤,因爲這是我想在這裏做什麼是我的主鍵,所以我決定在我的返回設備表單中有一個數據網格,所以如果用戶返回一個設備,我所要做的就是更新我的數據庫中的datereturned字段。你能幫我解碼嗎?

+0

回答信息不足 – 2010-10-03 05:00:00

+0

告訴我,我的發言缺乏什麼? – RedKing 2010-10-03 05:04:23

+1

我認爲你需要在桌子設計上做更多的工作。如果productnumber是主鍵,那麼它只能被「借用」和「返回」一次...... – 2010-10-03 13:23:17

回答

2

由於您沒有具體說明您需要什麼幫助,我將首先給您簡單的查詢來執行更新,然後我將向您展示如何與Access進行交互。

查詢更新datereturned:

str = "UPDATE 'Your Table Name' SET datereturned = " & now ' This is your update query. 

首先要做的就是創建一個連接Access數據庫:

 Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=D:\Tecnical Stu"& _ 
"dy\Complete_Code\Ch08\data\NorthWind.mdb" 
     Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString) 

找到在:

http://p2p.wrox.com/ado-net/28703-how-vbulletin-net-connect-access-database.html

下一頁你應該執行更新:

dbConnection .Open() 
str = "UPDATE 'Your Table Name' SET datereturned = " & now ' This is your update query. 
'string stores the command and CInt is used to convert number to string 
cmd = New OleDbCommand(str, cn) 
icount = cmd.ExecuteNonQuery 

這裏有一個類可以讓你輕鬆簡單地進行交互。

Imports System.Data.OleDb 
Public Class Form1 Inherits System.Windows.Forms.Form 
Dim cn As OleDbConnection 
Dim cmd As OleDbCommand 
Dim dr As OleDbDataReader 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e as _ 
System.EventArgs) Handles MyBase.Load 
End Sub 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_ 
System.EventArgs) Handles Button1.Click 
Try 
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;_ 
Data Source=C:\emp.mdb;") 
'provider to be used when working with access database 
cn.Open() 
cmd = New OleDbCommand("select * from table1", cn) 
dr = cmd.ExecuteReader 
While dr.Read() 
TextBox1.Text = dr(0) 
TextBox2.Text = dr(1) 
TextBox3.Text = dr(2) 
' loading data into TextBoxes by column index 
End While 
Catch 
End Try 
dr.Close() 
cn.Close() 
End Sub 
End Class 
When you run the code and click the Button, records from Table1 of the Emp database will be displayed in the TextBoxes. 

Retrieving records with a Console Application 

Imports System.Data.OleDb 
Imports System.Console 
Module Module1 

Dim cn As OleDbConnection 
Dim cmd As OleDbCommand 
Dim dr As OleDbDataReader 

Sub Main() 
Try 
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\emp.mdb;_ 
Persist Security Info=False") 
cn.Open() 
cmd = New OleDbCommand("select * from table1", cn) 
dr = cmd.ExecuteReader 
While dr.Read() 
WriteLine(dr(0)) 
WriteLine(dr(1)) 
WriteLine(dr(2)) 
'writing to console 
End While 
Catch 
End Try 
dr.Close() 
cn.Close() 
End Sub 

End Module 
Code for Inserting a Record 

Imports System.Data.OleDb 
Public Class Form2 Inherits System.Windows.Forms.Form 
Dim cn As OleDbConnection 
Dim cmd As OleDbCommand 
Dim dr As OleDbDataReader 
Dim icount As Integer 
Dim str As String 

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As_ 
System.EventArgs) Handles MyBase.Load 

End Sub 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_ 
System.EventArgs) Handles Button2.Click 
Try 
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\emp.mdb;") 
cn.Open() 
str = "insert into table1 values(" & CInt(TextBox1.Text) & ",'" & TextBox2.Text & "','" &_ 
TextBox3.Text & "')" 
'string stores the command and CInt is used to convert number to string 
cmd = New OleDbCommand(str, cn) 
icount = cmd.ExecuteNonQuery 
MessageBox.Show(icount) 
'displays number of records inserted 
Catch 
End Try 
cn.Close() 
End Sub 
End Class 

找到在:

http://www.startvbdotnet.com/ado/msaccess.aspx

+0

我應該爲這段代碼創建另一個模塊嗎?長長的? – RedKing 2010-10-03 15:34:55

+0

我通常會創建一個處理所有數據庫交互的類,但是您可能可以使用子實現簡單的更新。 – 2010-10-03 15:57:28

+0

圍繞表名反引號是什麼?這不是有效的Jet/ACE SQL,雖然也許你的數據接口層在將它們發送到Jet/ACE之前將它們刪除。我肯定會將它們排除在外,以防原始提問者的平臺實際上是VBA,而不是VB。 – 2010-10-03 18:36:29

1

你需要返回數據拆分的產品和借/成兩個表:

product: 
"productnumber" 
"productname" 

borrowed: 
borrowedID (use a simple autonumber) 
productnumber (foreign key linked to productnumber in the product table) 
"dateborrowed" 
"datereturned" 
"status" 
borrowerID (foreign key linked to borrower table) 

第三個表對借款人/客戶也將按順序排列

borrowers: 
borrowerID 
"borrowername" 
+0

爲什麼要拆分它們,如果datedate返回的是單個字段並且與初始表相關? – 2010-10-03 15:58:13