Sorry but I'm not very experienced when it comes to things like this.
I have a table called "Measure Table" with the fields "ID", "Proxy" and "ProxyID".
I created a form based on this table. ID is a label pre-populated from the table. Proxy is a drop down menu with the options "For" or "From". ProxyID contains a drop down with the same numbers as ID.
I would like a user to go to a specific record in the form (say for ID:I800), select "For" from the Proxy drop down and then select ProxyID (lets say L800). For the record for L800, I want it to automatically change the proxy to "From" and the ProxyID to I800.
Is this possible in Access?
Thanks so much for any help provided
Here is a visual of what i wnat to happen:
I want the table to look like this before the update(when the user selects "For" and "L800"):
Record# ID Proxy ProxyID
1 I800 For L800
2 L800
Then the table is automaticaly updated to:
Record# ID Proxy ProxyID
1 I800 For L800
2 L800 From I800
Okay, here is the gist of what you need to do to solve your immediate problem (updating the corresponding row in the other table.
Simply add an event handler to the AfterUpdate event of the form to perform the update to the other row. The code should look very similar to this...
Private Sub Form_AfterUpdate()
Dim RelatedID As String
Dim Proxy As String
If (UCase(Me.Form!Proxy) = "FOR") Then
RelatedID = Me.Form!ProxyID
CurrentID = Me.Form!ID
DoCmd.RunSQL ("UPDATE [Measure Table] SET ProxyID='" & CurrentID & "', Proxy='From' WHERE ID='" & RelatedID & "'")
End If
End Sub
Caveats:
As I mentioned in the comments, this data structure is a very bad idea and will create a lot of extra work for you to maintain the data integrity according to the implicit rules you are specifying as a matter of course with this design. I realize you have an existing DB to deal with, but frankly it would probably be less work to fix the DB design than maintain this one in the long run.
Some additional considerations you didn't ask about, but are going to need to deal with:
What happens if someone updates
either of the entries in a pair
directly in the table instead of
using your form? There really isn't a
good way to apply the above logic to run when
except in the context of using the form.
What happens in this code if the related row doesn't exist for some reason?
What happens if the related row "The FROM" row is updated in the form?
What happens if either row is deleted from the table?
Related
I am new-ish to access scripting, but through brute force and the Uni of Google/Microsoft, I have been able to make some pretty nice DB's. I have two items that I am struggling with.
I am creating a AFTER UPDATE macro from the table that will track the changes to the fields and create a new table with the changes "old" and "new" values. So far it works well for the most part, but I am trying to two specific things:
1 - capture the value from a CBO and not the ID number.
2 - run a function from my VBA script.
For the item #1 here is a sample of my code from the macro designer for a CBO value. (sorry don't know how to add the code itself)
If [Eng_Units] <> [Old].[Eng_Units]
RunDataMacro
Macro Name = tbl_UnityExport.dmcr_AuditUnity
Parameters
par_Event = "Edited"
par_ID = [ID]
par_Field = "Eng Units"
par_OldValue = [Old].[Eng_Units]
par_NewValue = [Eng_Units]
End If
For the par_OldValue and par_NewValue, my Eng_Units comes from another table called tbl_Eng_Units but what is being returned is the ID of the tbl_Eng_Units or column(0) and not the actual text in column(1) of table. The table that is recording the changes is my audit table and as you can see, ID numbers don't help me understand what I have changed from and to, unless I actually go in a look at the id in my Eng_Units table
Audit Record sample See #65
Can someone tell me how to get the textual value of the record and not the ID?
Item #2 I have another database that someone else developed and using a function in the "Value =" field of the SetField item. My function in a VB macro is called GetUserName(). and this is how it was written in the old DB. I am at a loss other than there is some other script I can't find...anywhere.
Macro Designer for Function
How would I run a function from the "Named Macro"
Your help and expertise is appreciated!
Forgive me if this has already been asked- I can’t seem to find a well written answer.
I am developing a small application for personal use.
Essentially what I have is two forms. Form 1 is a master view of all my contacts listed on a data grid view. Form 2 will be loaded on the cell/row double click of a particular record in order to edit it’s details.
My question is, what is the best practice/method for achieving this? I have seen many different methods.
Should I:
Pass only the primary key of the selected row then populate the fields on form 2 load
Pass all fields as a variable within a class then populate form 2 from that
Maybe I’m headed in the complete wrong direction though.
I have tried both ways, but wondering what the best method is for scalability.
My personal preference would be to pass a datarow into the opening argument of the form (rather than the PK / all the variables). You can then use the datarow inside your Form2 to bind to your controls or set their values, whichever you think is appropriate.
There are some useful examples on working with a datarow if you're unsure, alternatively you can also check out Microsoft Docs.
Public Sub New(ByVal row As DataRow)
InitializeComponent()
' your code for working with row here
End Sub
Edit:
In terms of "Binding" vs "Setting", you can either have your controls linked to your data to be two way (as you edit the data in a control at run time you alter the data in your database) or you can just set the values of the controls.
E.G. TextBox1.Text = row(0)("ColumnName")
You can find more on data binding on the Microsoft Docs page
This is probably a dumb question, but I'm a noob, so forgive me. I have two tables with a one to many relationship based on a OrderNumber. Essentially, I have some order details in one table, including OrderNumber (the one side of the relationship) and I am trying to get a list of LotNumbers in the other table using the OrderNumberLot (the many side) to link them together, as there can be many lot numbers on one order. The primary key in tblOrderDetails is OrderNumber and the primary key in tblLotNumber is LotID (auto number), but the linked field is OrderNumberLot. I made a open form button located on frmOrderDetail to open frmLotNumbers (based on the tblLotNumber). The deal is when frmLotNumber opens the OrderNumberLot field is blank. I understand there is no associated record in tblLotNumber, because I am trying to enter it, but how do I get the OrderNumber from the previous form, frmOrderDetail, with the button to automatically populate in the OrderNumberLot field in frmLotNumbers? I certainly don't want people to have to type it, because they will screw it up as badly as I have this explanation of my question! Thanks in advance for the help.
I agree with June7 on using a subform-approach for your purpose.
If you already have created the form, you could simply drag it from the sidebar onto your mainform and set the "Link Master Fields" and "Link Child Fields" to "OrderNumber" and "OrderNumberLot" - or you just use the subform-wizard and follow its instructions.
Though, some people dislike subforms in general and try to avoid them whenever possible - which might make sense at further stages of development. Instead of subforms, you could then use vba to transfer information from one form to another.
Lets say your button is named bt_openform
Private Sub bt_openform_Click()
'Open the desired form. The option "acFormAdd" forces the form
'to add a new entry whenever loading and requerying.
'As we want to add a new record, we will not use the filter settings
DoCmd.OpenForm "frmLotNumber", , , ,acFormAdd
'Next, store information from the initial form into the newly opened form
'To refer to data from the current form (meaning the one, in which the Sub is triggerd),
'you can use "Me.fieldname" and to refer to another form, use "Forms!formname!fieldname"
Forms!frmLotNumber!OrderNumberLot = Me.OrderNumber
End Sub
You could also do it the other way around and retrieve the information when loading the form:
Private Sub Form_Load()
Me.OrderNumberLot = Forms!frmOrderDetail!OrderNumber
End Sub
To be on the safe site, one might want to nest this function in an if statement, to check whether frmOrderDetail is open or not. This way, you can open your frmLotNumber from different forms without causing troubles
If CurrentProject.AllForms("frmOrderDetail").IsLoaded Then Me.OrderNumberLot = Forms!frmOrderDetail
I'm designing a database to track requests. Currently, I have a form that its' record source is based off a query "Unassigned Requests". This query is based off my table Requests, and returns all "unassigned Requests". In this form, I would like the status field to change to "Assigned", once a Tech field has been assigned to the request. I currently have the Default for the Tech Assigned field set to "Blank', and the status field set to 'Unassigned". Both of these fields are combo boxes. The status field has a control source from the request table and row source from the status table. The tech assigned field has a control source from the Tech table and the row source is based off a query.
I have tried multiple solutions that have not seemed to work. I have limited experience with Macros and VBA. I would appreciate any suggestions on solving this problem.
I am going to make some assumptions regarding your post and if any are incorrect please let me know.
You have a request form where a request is assigned to a Tech. On that form there is a dropdown for the status, and also for the Tech.
You want to make it so when the tech dropdown is filled out with a tech's name you want the dropdown to change to assigned.
If this is the case I would recommend using the Tech Assigned field's AfterUpdate event. The code would look something like this:
Private Sub cboTech_Assigned_AfterUpdate()
If Nz(Me.cboTech_Assigned.Value, "") <> "" Then
Me.cboStatus = "Assigned"
Else
Me.cboStatus = ""
End If
End Sub
Obviously you will need to adjust to your own naming scheme. I should also point out that I don't even know if that Nz function is needed, I have just gotten into the habit of putting it everywhere. If I misunderstood something about what you want to do please let me know!
The 1,500 page Access 97 Bible (don't laugh!) that I've been given by my boss to solve his problem doesn't solve my problem of how to solve his problem, because it has nee VBA code.
Let me first make clear that I've made attempts to solve this without (much) coding, and that I've coded quite a bit in VBA already, so I'm basically familiar with most things including recordsets, queries, etc etc but have problems with MS Access limits on how to form a report with data coming from VBA variables. I'm also versatile in most programming languages, but this is not a language problem but rather a "how to/what's possible" problem.
My problem right now is that dragging the query fields into the Detail subform and putting them into cells in columns setting Left and Top with VBA code are moving them alright, but each cell is on a new page. Unfortunately, there is multiple data in each cell that won't conform to the Create Report Guide options available.
So my question is simply this: Can someone point me to working examples of code that create, place, and fill with VBA variable strings, text fields at any coordinate I please on a paper size of my choice?
Edit: The above is not an option, as I understand this will prohibit the client from getting an .mde database. What remains, then, is to merely ask for some sound advice on how to get several rows GROUPed BY weekday and machine (see below) into a recordset or similar for each cell. I guess the best way is to count the number of columns in the table (machines in the sql result) and create 5 rows of these with dummy data, then go through the result rows and place the data in the relevant controls. But if you have ideas for doing this work better and faster, write them as answers.
Sorry for this, I knew there was something I wasn't understanding. Basically, I thought Access supported creating reports dynamically via VBA, ie. "generating pages with data" rather than "preparing a flow of controls connected to datasources". But Access requires that you create an ample amount of dummy, unlinked controls manually, then either fill or hide them and that's how they become "dynamic".
This is for Access 2003 on a remote server accessing local and remote ODBC SQL database tables, if relevant. The goal is to make a week schedule of n columns (n=number of machines at a certain plant) x 5 rows (weekday Mon-Fri), and put 1 or more recordset rows (=scheduled activities for that day on that machine) in each of the "n by 5 table" cells.
If you detect venting frustration in this post I can only ask your forgiveness and hope for your understanding.
So, has many techniques for this:
Ex: 1) using dinamic sql for this:
'Create a function to make sql query
Function MakeMySQlReport(Parameters):
Dim strSql as string
Dim strMyVar as string
strsql = vbnullstring
strsql = "Select " & myVar1 & " as MyFieldVar1, * from myTable where Fieldx =" & Parameters
MyReport.recordSource = ssql
End Function
Ex: 2) create function that returns yours strings:
Function MyString1() as string
MyString1 = 'ABC'
end Function
An in your report, select the textbox will receive the value and type =MyString1()]
I hope this help to you, need more examples?
Solution:
Create many objects manually (grr!)
name them systematically
put them in a Control Array (get all Me.Controls, sift out the ones you're interested in, and put them in an indexed array)
go through the array and change their properties