MS Access 2007: unable to change fields after "code opening" a form - forms

I'm fighting with creation of several forms in MS Access 2007.
I accomplished the following: I have patients form, where I can create / edit patient records. When save button is pressed, I'm opening another form, that has a task of entering information that belong to 2 tables. Tables on that form are in 1-1 relationship, and both use foreign key (patiendID from patients table).
I managed to make everything work fine - when I update / save new patient, I have a new form opened with a bunch of lab results to be entered. Some of the results belong to one table, and other to another table. patientID field, that is also visible on that 2nd form is set as it should be. However, when I try to enter ANY value in ANY field on that form - I get following warning on the status bar: "This Recordset is not updateable".
I think that this has smt to do with the fact that I actually opened 2 tables on a single form, but I might be very wrong.
Here is the code I use to open 2nd form:
Private Sub save_Click()
Dim m_query As String
m_query = "INSERT INTO labresults (patientID) VALUES (" & Me.ID & ")"
If Me.Dirty = True Then
Me.Dirty = False
End If
If DCount("patientID", "labresults", "patientID = " & Me.ID) = 0 Then
CurrentDb.Execute m_query, dbFailOnError
End If
m_query = "INSERT INTO par14MO (patientID) VALUES (" & Me.ID & ")"
If DCount("patientID", "par14MO", "patientID = " & Me.ID) = 0 Then
CurrentDb.Execute m_query, dbFailOnError
End If
If CurrentProject.AllForms("labresults").IsLoaded = True Then
Forms![labresults]![patientID] = Me.ID
Forms![par14MO]![patientID] = Me.ID
Else
DoCmd.OpenForm "labresults", acNormal, , "idPAcijenta = " & Me.ID, acFormEdit, acWindowNormal, Me.ID
End If
End Sub
Any ideas what's going on???
Thx a bunch!
I'm still googling and trying... I'll post my findings if I manage to sort things out!

Turns out that this really cannot be done :). Closing this one, and assuming this is an answer.
Edit: It was suggested to me that my last statement is not true. However, I solved things "manually", and everything is ok now. I'll go on and accept this answer in order to keep my response at a good level.

Related

MS Access VBA DoCmd.OpenForm using where clause not filtering DAO query based recordset

I,m trying to get rid of linked tables and use only VBA code to generate recordset. I found that filtering data using where clause in my DoCmd.OpenForm command doesn't work this way. Is that expected behavior? Or maybe it should work and the problem is located somewhere else... Are OpenArgs the only thing that left me to do this?
To clarify my question:
I have two ms access forms:
One (continuous form) with hyperlink and on click code behind like follows
Private Sub txtPerson_Click()
DoCmd.OpenForm "frmPersonnelDetails", , , "PersonId = " & Me.txtPersonID, acFormReadOnly, acDialog
End Sub
and second one (frmPersonnelDetails), not connected to any recordsource, with recordset created with:
Private Sub Form_Load()
strQuery = "SELECT PersonID, Abbreviation, FirstName, LastName FROM SomeTable"
Set objDaoDb = GetDAODbConn 'function that returns database connection object
Set objDaoRS = objDaoDb.OpenRecordset(strQuery, dbOpenDynaset, dbSeeChanges)
Set Me.Form.Recordset = objDaoRS
End Sub
Now, where clause doesn't work. Second form is opening always on the first record. Is it normal? What is the best way to make it open on specified record?

Why is Access form giving me the error message "A column number has been specified more than once in the order list"? The related queries open fine

I'm working on an Access database with a SQL Server back end. Some times when I open a particular form, I get an Error 3146 "Column has been specified more than once in the order by list."
There are several possible queries that might feed into this form but I checked and ran them all individually and they run fine, so it's something in the form but I'm not sure. Below is a screen shot of the Data Properties window. My hypothesis is that "Order By" property (which I did not populate manually) is not being cleared out when the form closes, and thus when the form is re-opened, that in effect puts in the same columns twice.
I can temporarily solve the problem by opening up the form and clearing those Filter and Order By fields. That solves it for a while but intermittently it will happen again so there's probably one other thing I'm overlooking. I put in code in the Form close event to set the orderby="" but even that doesn't totally solve it.
Here is the code that opens the form from the main menu (frmProdSched is the form in question)
Private Function OpenProdSched(RunQuery, RunCaption)
DoCmd.OpenForm "frmProdSched", , RunQuery, "", , acNormal
Forms!frmProdSched.Caption = RunCaption
gScheduleRptQry = RunQuery
Me.Refresh
End Function
and then the code that is in the Form_Current event
Private Sub Form_Current()
On Error Resume Next
Dim strForm As String
Dim strForm1 As String
Dim strWhere As String
strForm = "frmSubmittalStatus"
strForm1 = "frmProdSched"
strWhere = "StatusJobno = " & Me.JOBNO & ""
If IsLoaded(strForm) Then
If Forms(strForm).Filter = strWhere Then
Forms(strForm1).SetFocus
Else
DoCmd.Close acForm, strForm
DoCmd.OpenForm FormName:=strForm, WhereCondition:=strWhere
Forms(strForm1).SetFocus
End If
End If
End Sub
I tried adding this code to the Form Close event and it seemed to work most of the time but it still intermittently happens
Private Sub Form_Close()
Me.Filter = ""
Me.OrderBy = ""
End Sub
I'm not sure what to check next. Any ideas?

MS Access Multiple Instances of Single Form for different clients

Another question that stumps me.
I have a continuous form that shows a list of all of our clients at the law firm I work at here. Right now you can double click on a client name where a form (frmContactSummary) then opens up to display all the information for that client.
Problem is, as it is currently designed only one form can be open for a client at a time.
We want to be able to open multiple versions or instances of frmContactSummary.
I borrowed code from Allen Browne's site, which is as follows:
Option Compare Database
Option Explicit
'Author: Allen J Browne, July 2004
'Email: allen#allenbrowne.com
'Found at: http://allenbrowne.com/ser-35.html
Public clnClient As New Collection 'Instances of frmClient.
Function OpenAClient() 'ContactID As Integer
'Purpose: Open an independent instance of form frmClient.
Dim frm As Form
'Debug.Print "ID: " & ID
'Open a new instance, show it, and set a caption.
Set frm = New Form_frmContactSummary
frm.Visible = True
frm.Caption = frm.Hwnd & ", opened " & Now()
'Append it to our collection.
clnClient.Add Item:=frm, Key:=CStr(frm.Hwnd)
Set frm = Nothing
End Function
This works in a way, but it only opens up the first record in our Contact table. We want the instance to open on a specific record, or ID from the Contacts table.
I tried this code near the end:
frm.RecordSource = "select * from Contacts where [ID] = " & ContactID
But it did not work.. :-(
Any advise would be much appreciated! Thank you!
Ok, when you create a instance of a form, you can’t use the common approach of a where clause like this:
Docmd.OpenForm "frmContactSummary",,,"id = " & me!id
The above of course would work for opening one form.
However, in your case, you need:
Create a new instance of the form
Move/set the form recordsource to ID
Display the form
So we need a means to move or set the form to the ID of the row we just clicked on.
So right after we create the form, then add this line of code:
Set frm = New Form_frmContactSummary
Frm.RecordSource = "select * from Contacts where id = " & me!id
And rest of your code follows.
It not clear if the PK (key) of the table Contacts is “id”, or “ContactID”
So your code will be:
Frm.RecordSource = "select * from Contacts where id = " & me!id
Or
Frm.RecordSource = "select * from Contacts where Contactid = " & me!ContactID
Simply replace “ContactID” in above with a actual PK id used in table contacts. The "Id" has nothing to do with the collection. We are simply building a SQL statement that will pull/set the form to the one row. So the only information required here is what is the PK name in your continues form, and what is the PK name in your frmContactsSummary. (they will be the same name in both forms, and should thus be the same in the sql statement.

Utilizing Access and VBA: copying information between forms

Searched around a bit and could not find an existing post with my exact question.
I am creating an Access (2003) database utilizing multiple forms and tables that need to communicate and share information with one another (i.e. - first/last name, etc.). The main form/table, "Personnel", will hold a majority of the information, yet the other forms/tables will only hold information pertinent to certain situations/circumstances, so not every entry in "Personnel" will have a concurrent entry in "Hired", for example. If a record with the same name already exists, I would like a MsgBox to tell me so and open that entry (in the form, for editing), if not a new entry would be created, auto-populated with pre-selected fields (i.e. - first/last name).
After an option is selected from a pull-down menu, the appropriate form is accessed/opened, "Hired", for example (This part works).
The copying information across forms does not work.
Dim rstPers, rstHired As DAO.recordset
Dim LastName As String
If status = "hired" Then
DoCmd.OpenForm "Hired Information"
Set rstPers Forms!Personnel.RecordsetClone
Set rstHired Forms![Hired Information].RecordsetClone
????
...
End If
I have tried to do this multiple ways but nothing seems to work. Nothing populates in the new Form or table. Am I looking at it the wrong way? Should I try a different approach?
Hope my explanation makes sense. Thanks for any help.
-Charles
You have a bad approach indeed.
Your forms are linked to a table. So you should avoid as much as possible to manipulate or retrieve a form's data directly using a recordsetclone, instead try to retrieve this data directly from the table.
So if you want to open your "hired information" form:
Dim RS As Recordset
Dim IDperson As String
IDperson = me.ID ' or whatever
Set RS = CurrentDb.OpenRecordset("SELECT ID FROM TableHired WHERE ID='" & IDperson & "'")
If RS.BOF Then
' No record exist - Open form in record creation
DoCmd.OpenForm "FormHired", acNormal, , , acFormAdd
Else
' record exists, open form and position it oon the record's ID
DoCmd.OpenForm "FormHired", acNormal, , "ID='" & RS!ID & "'", acFormEdit
End If
Of course it won't work like this as you didn't provide enough info.
Review this code and adapt it with your fields name (IDs), table name (TableHired) and FormName (FormHired) and following the situation on where and how you will trigger it. Also if your ID is not a string, you should remove the quotes in the SQL

Enable Save button on different tabs when a form opens

I have a tab control on a form, and a couple different tabs have save buttons on them. Once the user saves data (via SQL statements in VBA), I set the .enabled = false so that they cannot use this button again until moving to a brand new record (which is a button click on the overall form).
so when my form open i was going to reference a sub that enabled all these save buttons because the open event would mean new record. though i get an error that says it either does not exist, or is closed.
any ideas?
thanks
EDIT:
Sub Example()
error handling
Dim db as dao.database
dim rs as dao.recordset
dim sql as string
SQL = "INSERT INTO tblMain (Name, Address, CITY) VALUES ("
if not isnull (me.name) then
sql = sql & """" & me.name & ""","
else
sql = sql & " NULL,"
end if
if not insull(me.adress) then
sql = sql & " """ & me.address & ""","
else
sql = sql & " NULL,"
end if
if not isnull(me.city) then
sql = sql & " """ & me.city & ""","
else
sql = sql & " NULL,"
end if
'debug.print(sql)
set db = currentdb
db.execute (sql)
MsgBox "Changes were successfully saved"
me.MyTabCtl.Pages.Item("SecondPage").setfocus
me.cmdSaveInfo.enabled = false
and then on then the cmdSave needs to get re enabled on a new record (which by the way, this form is unbound), so it all happens when the form is re-opened. I tried this:
Sub Form_Open()
me.cmdSaveInfo.enabled = true
End Sub
and this is where I get the error stated above. So this is also not the tab that has focus when the form opens. Is that why I get this error? I cannot enable or disable a control when the tab is not showing?
You cannot use the form open event to manipulate controls, they have not been initiated at that stage. Use the form load event.
It should never be necessary to set focus to a tab, or even to reference it when working with a control. You will notice that controls must have names unique to the form, and adding a tab does not change this.
I suggest you set some form-level variables: booBtn_1_enabled as Boolean, booBtn_2_enabled as Boolean. Set these to T or F as needed; obviously, all T when the form is opened. Pick a form event (possibly the Current event, but preferably one that is triggered less often) that reviews these variables and sets the controls accordingly:
Me.btnBtn_1.Enabled = booBtn_1_enabled
Me.Repaint
Something like that, but obviously Me.btnBtn_1 may need a more complicated reference.