English 中文(简体)
如何从一个表格(Access 2007)直接更新数据库的记录
原标题:How to directly update a record in a database from a form number (Access 2007)

I have a job-tracking system, and there is a query that returns results of all jobs that are overdue. I have a form that displays each of these jobs one-by-one, and has two buttons (Job has been completed, and Job not completed). Not completed simply shows the next record. I cannot find a way to get access to the current record to update it s contents if the "Has been Completed" button is pressed, the closest I can get is the long number which represents the records position in the form.

The VBA to get the index of the record in the form is as follows.

Sub Jobcompleted(frm As Form)
    Dim curr_rec_num As Long
    curr_rec_num = frm.CurrentRecord
End Sub

This is my first shot at VBA, and after an hour of searching I cannot find anything to solve my problem. Am I going about this the entirely wrong way? Working in Microsoft Access 2007

<<>Further 所有表格都是正常的。

车辆表

就业情况表: 工作岗位——id(pk)、汽车(id)(fk)和其他信息,说明需要做什么,以及下一个工作日期、工作间隔(所有工作重复)和其他信息

工作史表: 工作岗位——理论学(pk)、工作(id(fk)、完成日期和评论

When the job completed button is pressed, it should create a new entry in the job history table with the current date, any comments and the job id

This is the script I am trying to get working

Private Sub Command29_Click()
    Dim strSQL1 As String
    Dim strSQL2 As String
    Set Rs = CurrentRs
    Set db = CurrentDb

    strSQL1 = "INSERT INTO completed_jobs(JOB_ID, DATE_COMPLETED, COMMENTS) VALUES " & Rs!job.ID & ", " & Date
    db.Execute strSQL1, dbFailOnError
    strSQL2 = "UPDATE job SET JOB_NEXT_OCCURANCE = JOB_NEXT_OCCURANCE+JOB_RECURRANCE_RATE WHERE job.ID = Rs!job.ID"
    db.Execute strSQL2, dbFailOnError
End Sub

注:线路<代码>Set Rs = CurrentRs完全不正确,我认为这是我需要说明的。 这一点呼吁纽芬兰语

我的图像显示形式(非连续性)。

@HansUp,我得到你所说的话,但我原想完全认为它适用(我没有首次为你们提供足够信息,以理解我的想法)。

@sarh 我认为,你所谈论的是,我需要的是什么,但我无法说明如何使用它,以及任何方面?

@Matt,我有90%相信我正在使用约束性表格(我说,新到来的是,我一直在研究人们所建议的一切,并学习我。) 当然,有一份工作证明(没有显示,没有必要看到),但我如何利用这个身份开展行动? 我可以这样做,与进出/签证局融合在一起。

Form for ticking off jobs as they are completed, calls code shown

最佳回答

我理解你的情况,你的形式是数据约束(你可以取得记录索引),因此,你的形式已经放在了这一记录上。 如果你需要更新一些基本数据集,你可以撰写一些像样的东西。

Me!SomeField = ...
DoCmd.RunCommand acCmdSaveRecord

If your form has control bound to "SomeField", then the form will be updated automatically.

如果这无助于,你可以找到另一个方向:

(1) 使用SQ代码更新记录。 例如,你有记录证明,应以数据集的形式加以更新,以便你能够写出如下内容:

Call CurrentDB.Execute( _
"UPDATE SomeTable SET SomeField = SomeValue WHERE SomeTableID = " & Me!SomeTableID, dbSeeChanges)

2) 您可以看看看书记财产——记录和表格都有这一财产,描述记录状况。 因此,你可以提出这样的内容(不是最好的例子,但可以帮助你们找到想法):

Dim Rs as Recordset
Set Rs = Me.RecordsetClone  make a reference copy of the form recordset
Rs.Bookmark = Me.Bookmark  locate this recordset to the form current record
问题回答

考虑更简单的办法。 我怀疑你需要关注当前记录财产的形式。 而我看不出为什么你们需要一个“已经完成”的指挥区,而另一个则需要“没有完成”。

Add a "Yes/No" data type field to the table which is used by your form s record source. Set it s default value property to 0, which represents False or No. Call it "completion_status". Create a new form using that record source. Then your form can have a check box control for completion_status.

Newly added records will have False/No as completion_status --- the check box will appear unchecked. The completion_status for other records in the forms can be toggled between Yes (checked) and No (unchecked) using the check box control.





相关问题
Handling no results for docmd.applyfilter

I have an Access app where I use search functionality. I have a TextBox and a Search Button on the form, and it does a wildcard search of whatever the user enters in the TextBox, and displays the ...

Outlook 2007 CommandBarControl.Execute won t work

I recently switched to Outlook 2007 and noticed that my VBA-macros won t work. I use the following code to open a new appointment-item (and fill it automatically). It worked perfect in Outlook 2003, ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

MS Access: list macro from VBA

I have to deal with a few macros (not VBA) in an inherited Access application. In order to document them, I would like to print or list the actions in those macros, but I am very dissatisfied by ...

热门标签