How do i set values based on other values when database opens - ms-access

I am trying to get the status of my jobs to change automatically when the database opens.
If Me.STATUS = 6 And Me.UPDATED_TIME = Date - 1 Then
Me.STATUS = 2
Else
End If
that is my current code however it doesn't seem to be working...
help please!

as explained by Erik von Asmuth, vba was not the proper way to complete the task I was wanting. Below is the link to where I found the how to on an "Update Query"
https://support.office.com/en-us/article/Create-and-run-an-update-query-9dddc97c-f17d-43f4-a729-35e5ee1e0514

Related

Dynamic Month filter at form start

I got a filter for my form, where I can choose a month - all datas, related to that month will then be shown. Translation: Datenauswahl = Choose month
My Problem is, 90% of the time, the user wants to work at the current month and dont want to see past datas.
Everytime the user is switching a tab at the navigation form, the filter resets.
So tried Default value of my combobox: Format(Datum();"mm")
Result: Current month input: Yes | Filter: No. So there have to be a possibillity, that after the default value has been loaded, the form checks the value of the combobox and filter it, everytime when swichting navigation tabs or On Load.
At my form, I activated Filter at Load.
The Filter of the Form itself is: Format([tbl_taetigkeitserfassung.TaetigkeitsDatum], 'mm') = '11' -> bc of the combobox. A Value like: Format([tbl_taetigkeitserfassung.TaetigkeitsDatum], 'mm') = Format(Datum();'mm') Is not working
I would appreciate some help or ideas to improve my user experience
BTW: code of the combobox, if needed:
Private Sub Kombinationsfeld479_AfterUpdate()
If Me.Kombinationsfeld479 = "Alle" Then
Me.FilterOn = False
ElseIf Not IsNull(Me.Kombinationsfeld479) Then
Me.Filter = "Format([tbl_taetigkeitserfassung.TaetigkeitsDatum], 'mm')='" & Me.Kombinationsfeld479 & "'"
Me.FilterOn = True
End If
End Sub
Ok, after some test I can say, that the following combination of form properties works fine here. The form automatically applies the filter when opening.
Filter On Load: Yes
Filter: Format([TaetigkeitsDatum],"mm")=Format(Date(),"mm")
So it looks like tbl_taetigkeitserfassung. is wrong in this case and should be removed. Also take care to replace the ; by a , like in my example.
First of all will your database never have more than one year's worth of data? Your current filtering process will stop working once you have data from more than one year. Use "yyyy-mm" date format to account for that.
For your filtering to work as you want you must follow every change in the filter (or even enabling it or disabling it) with Me.Requery so that the form updates based on the new filter.

HP QC 12 and MS Access Query

I'm currently trying to figure out a way to connect to HP QC 12 via MS Access and import the data found in table bug via a simple query. However, I'm having a hard time getting the HP QC data into a usable query.
I've tried two ways so far:
Public Function import_HPQC()
Dim QCConnection
Dim Com
Dim HPQC_RST As TDAPIOLELib.Recordset
Set QCConnection = CreateObject("TDApiOle80.TDConnection")
QCConnection.InitConnectionEx <>
QCConnection.login <>, <>)
QCConnection.Connect <>, <>
QCConnection.IgnoreHTMLFormat = True
Com.CommandText = "SELECT * FROM bug"
Com.Execute
Set HPQC_RST = Com.Execute
rcount = 0
HPQC_RST.First
Do Until HPQC_RST.EOR
rcount = rcount + 1
HPQC_RST.Next
Loop
MsgBox (rcount)
QCConnection.Logout
End Function
This first way works in the way that I'm able to at least get an accurate record count of what is contained in bug but I can't get past this part. I can't figure out a way to simply run an insert into against HPQC_RST.
The other way I've tried is basically the same, but instead of a select query I'm attempting to define BugFactory.NewList("") as a recordset but that just isn't working either. I'm able to loop through a BugList in the following fashion but I think it's a really messy solution and I would much rather simply append everything from bug directly into an MS Access table.
Set BugFactory = QCConnection.BugFactory
Set BugList = BugFactory.NewList(“”)
For Each Bug In BugList
HPQC_Table.AddNew
HPQC_Table![ID] = Bug.Field("BG_BUG_ID")
HPQC_Table.Update
Next
Any help would be greatly appreciated as I've been working on this for a few days and have made very little progress.
You're connecting to the API, not to the actual database. If the API only provides recordset-like objects that you can loop over, that's probably what you will have to do.
(/me smiling a little at the remnants of TestDirector in the object names)

Wildcard in Access VBA

This is kind of a follow up question to this post: Access VBA recordset string comparison not working with wildcard but I don't have the rep to answer/comment on it to ask it in house. What I'm curious about is this line of code specifically:
If ![ACOD] = "*_X" Then '"$ICP_X" works
Debug.Print ![ACOD] 'For testing
'.Delete
End If
I want to know if this can be modified so that on a button click, it looks at all fields in a form with the field name of *_New (with the hope to catch all fields where the name ends in _New) and if they are not Null then confirm that the user wanted to make a the change indicated in the field. I was thinking of something along the lines like this:
If Not isNull(*_New.value) Then
If Msgbox ("Do you want to make these changes?",vbOKCancel, "Confirm Changes") = 1 Then
'### Do something with the record ###
End If
End If
EDIT
As of posting the above information, I did not have the Microsoft VBScript Regular Expressions Reference installed, currently I have version 5.5 (it was the latest version). With that installed (referenced?) and seeing the information from this site MS Access with VBA Regex, I'm wondering if it's better to do something like this:
Dim re As RegExp
Set re = New RegExp
re.IgnoreCase = True
re.Global = True
re.Pattern = "*_New"
If ##Not sure on syntax to match all fields## Then
Msgbox(##Same stuff as above MsgBox##)
End If
EDIT 2
Here's a sample case for my form I'm working on. Each of the fields to the right have names that end in _New. What I want to do is on the button click, to check and see what fields on the right have been filled in and ask the user if they want to confirm the changes to the record.
Not sure what you are trying to achieve but there is a way to access the control collection in a form. Here is a public function where you can loop through all controls and check its name.
Public Function FN_CONFIRM_CHANGES(iSender As Form)
Dim mCtl As control
For Each mCtl In iSender
If VBA.Right(mCtl.name, 4) = "_New" Then
Debug.Print mCtl.name & " is a match and its a " & VBA.TypeName(mCtl)
End If
Next mCtl
End Function
Call this function like
FN_CONFIRM_CHANGES Me 'Where me is referencing the form you are in.
You can modify the above code to return a boolean value to stop further execution if user decided not to save your changes or whatever logic you are trying to implement.

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 Info All tables are normalized
Vehicle Table: Contains vehicle_id(pk), as well as rego and model etc
Job Table: Contains job_id(pk), vehicle_id(fk) and other info about what needs to happen, as well as the next occurance date, days between each occurance of the job (all jobs repeat) and other info
Job History Table: Contains job_history_id(pk), job_id(fk), date completed and comments
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
Note: Line Set Rs = CurrentRs is completely incorrect, I believe this is what I need to figure out? This is called on button-press
I am posting an image which shows the form (non-continuous).
#HansUp, I get what you are saying, but I dont quite think it's applicable (I did not provide enough information first time around for you to understand I think)
#sarh I believe this Recordset that you are talking about is what I need, however I cannot figure out how to use it, any hints?
#Matt I am 90% sure I am using a bound form (Like I said, new to Access, been looking at everything people have suggested and learning as I go). There is of course an ID for the job (Just not shown, no need to be visible), but how would I access this to perform an operation on it? SQL I can do, integrating with Access/VBA I am new at
As I understand your situation, your form is data-bound bound (you can get record index), so - your form already located on this record. If you need to update some field of underlying dataset, you can write something like
Me!SomeField = ...
DoCmd.RunCommand acCmdSaveRecord
If your form has control bound to "SomeField", then the form will be updated automatically.
If this will not help, you can look to a couple of another directions:
1) Update records using SQL code. For example, you have ID of record that should be updated in the form data set, so you can write something like:
Call CurrentDB.Execute( _
"UPDATE SomeTable SET SomeField = SomeValue WHERE SomeTableID = " & Me!SomeTableID, dbSeeChanges)
2) You can look at the Bookmark property - both Recordset and Form has this property, it describes the record position. So you can write something like this (not the best example, but can help you to get an idea):
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
Consider a simpler approach. I doubt you need to be concerned with the form's CurrentRecord property. And I don't see why you should need a command button for "Has been Completed" and another for "Has not been Completed".
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.

Pivot Chart Creation Using Access 2007 VBA

OK there are absolutely no good articles on the internet that I can find that explain or have code examples on how to create a pivot chart using VBA. I need this because I need the pivot chart to show different results depending on user selection in a form. There are some for Excel but the syntax is different for Access. I know this is a lame question, but if anyone has an example of how to create a pivot chart in VBA I would really appreciate the help.
Well after about 3 days of searching I think I found it. Not that anyone really cares, this only has like 6 views, says a lot for VBA's utter horribleness. Anyway, MSDN had this hidden under "Office XP" instead of under Access, but whatever.
http://msdn.microsoft.com/en-us/library/aa662945.aspx#
I've create a PivotChart form in Access 2007. On another form I have the user selection controls and the pivotform as a subform. Then I use code like this in the main form. The object model is the same as OWC11 (Office Web Components 11).
Private Function DisplayChart()
With mysubform.Form.ChartSpace
.Clear
.AllowFiltering = True
.HasChartSpaceTitle = True
.ChartSpaceTitle.Caption = "test"
.DisplayFieldButtons = False
.DisplayToolbar = False
.ConnectionString = ...
.CommandText = "SELECT rSeries, rCategory, rDate, rValue " & _
"FROM myTable"
.Charts(0).Type = chChartTypePie
.SetData chDimSeriesNames, chDataBound, "rSeries"
.SetData chDimCategories, chDataBound, "rCategory"
.SetData chDimValues, chDataBound, "rValue"
.HasChartSpaceLegend = True
End With
End Function
the constants can be derived from OWC11
C:\Program Files\Common Files\Microsoft Shared\Web Components\11\OWC11.DLL
You need them at the top of the module. At this stage I'm not sure how to extract them from the Access pivotchart. Make a reference to OWC11 and set the subform ChartSpace to a variable declared as an OWC11.ChartSpace. After writing the code change to type 'Object', and remove the reference for late binding (and re-test). This way your refs won't come unstuck on a 64bit machine when you deploy.
Private Enum ChartConstants
chDimSeriesNames = 0
chDimCategories = 1
chDimValues = 2
chDataBound = 0
chAxisPositionValue = -8
chAxisPositionCategory = -7
chChartTypePie = 18
End Enum
Remember you can also let the user have access to the PivotChart properties form, field lists and drop zones. Or they can right-click the chart to get to them.
(Note - this is still a new discovery for me so I will endeavor to update this answer if I find any gotcha's.)