Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access 通过visual basic更新microsoft access 2000中的字段_Ms Access - Fatal编程技术网

Ms access 通过visual basic更新microsoft access 2000中的字段

Ms access 通过visual basic更新microsoft access 2000中的字段,ms-access,Ms Access,我创建了一个添加和返回设备的程序,我的问题是,这是因为我的借用表和返回设备表仅在一个表中。表示这些字段 “产品编号” “产品名称” “借来的日期” “已返回日期” “借用名称” “地位” 我想在这里做的是,当用户返回设备时,在字段中输入相同的数据会在我的数据库中出错..尤其是在我的productnumber上,因为这是我的主键,所以我决定在我的返回设备表单中有一个数据网格,因此如果用户返回设备,我要做的就是更新数据库中的datereturned字段..伙计们?你能帮我处理代码吗?由于你没有明确指

我创建了一个添加和返回设备的程序,我的问题是,这是因为我的借用表和返回设备表仅在一个表中。表示这些字段

“产品编号”

“产品名称”

“借来的日期”

“已返回日期”

“借用名称”

“地位”


我想在这里做的是,当用户返回设备时,在字段中输入相同的数据会在我的数据库中出错..尤其是在我的productnumber上,因为这是我的主键,所以我决定在我的返回设备表单中有一个数据网格,因此如果用户返回设备,我要做的就是更新数据库中的datereturned字段..伙计们?你能帮我处理代码吗?

由于你没有明确指出你具体需要什么帮助,我将首先向你提供执行更新的简单查询,然后我将向你展示如何与Access交互

返回了要更新日期的查询:

  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 )
网址:

接下来,您应该执行更新:

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
网址:


您需要将产品和借用/归还数据拆分为两个表:

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"

没有足够的信息来回答告诉我,我的陈述中缺少什么?我认为你需要在你的桌子设计上做更多的工作。如果productnumber是主键,则只能“借用”和“返回”一次…Peter勋爵是正确的,您需要一个唯一的主键,该主键独立于productnumber、可能的借用id或对每个条目自动递增的某个字段。我通常创建一个处理所有数据库交互的类,但是您可能可以使用sub完成简单的更新。表名周围的倒勾是什么?这不是有效的Jet/ACE SQL,尽管您的数据接口层可能会在将它们发送到Jet/ACE之前删除它们。如果原始提问者的平台实际上是VBA而不是VB,我当然会把它们删掉。反勾号确保数据库理解这是一个字段。@David-W-Fenton感谢你提醒我注意,我在这里包含了反勾号,因为Access不喜欢这种语法,如果datereturned是单个字段并且与初始表直接相关,那么为什么要拆分它们呢?