Thanks Rod,
Sorry to trouble you.
I have set up 3 forms now.
The original to add a complete new quote, one to edit a quote and one to
open from the edit form to copy the data and create a new revision, so no
form is taking focus or trying to add data whilst the other is open.
The VBA for the "OnOpen" in the 3rd form for coping now reads,
Dim dbs As Database
Dim rst As Recordset
Dim strsql As String
Set dbs = CurrentDb
strsql = "SELECT * FROM [Sales Enquiry Register] "
strsql = strsql & "WHERE [Enquiry/Quote] = " & CLng(Me.OpenArgs)
Set rst = dbs.OpenRecordset(strsql, dbOpenSnapshot)
If Me.OpenArgs And Len(Trim(Nz(Me.OpenArgs, ""))) > 0 Then
'do something
Me![Reference] = rst.Fields![Enquiry/Quote]
Else
'do something
End If
added the space after the bracket and the other change.
A runtime error now occurs at
Me![Reference] = rst.Fields![Enquiry/Quote]
"You cannot assign a value to this object"
I have tried
Me![CompanyName] = rst.Fields![CompanyName]
Which is another field to which data would also be copied - with the same
result and error message with the same message.
Any help appreciated as always.
I've used open args before to copy information from an underlying form to a
completely unrelated form with its own table.
This time I am trying to use a common table and take a set of data and add
it too a new line in in the table so to speak and things arn't going
according to plan.
--
Warm Regards
Bill
:
Hi Bill,
There're a couple of errors with your strsql assignment.
First you should include a space after the final bracket on the first line.
As it is, when the lines are concatenated the keyword WHERE will abut the
bracket and I don't know what the SQL parser will make of that.
What is probably causing the error message is the second line. This should be
"WHERE [Enquiry/Quote] = " & CLng(Me.OpenArgs)
Note that the literal part ends after the equals sign to which is
concatenanted the value of OpenArgs converted to a numeric long data type.
Get back if this does not work.
Rod
:
Hi Rod,
I've tried the follwing,
Dim dbs As Database
Dim rst As Recordset
Dim strsql As String
Set dbs = CurrentDb
strsql = "SELECT * FROM [Sales Enquiry Register]"
strsql = strsql & "WHERE [Enquiry/Quote] = openargs"
Set rst = dbs.OpenRecordset(strsql, dbOpenSnapshot)
If Me.OpenArgs And Len(Trim(Nz(Me.OpenArgs, ""))) > 0 Then
'do something
Me![Reference] = rst.Fields![Enquiry/Quote]
Else
'do something
End If
It is in the "Onopen" of the form.
I'm getting too few parameters - expect one in the follwing line.
Set rst = dbs.OpenRecordset(strsql, dbOpenSnapshot)
I'm giving it a go - any ideas?
--
Warm Regards
Bill
:
Hi Bill,
Hope you enjoyed my diatribe in response to one of your other posts.
Let's take this step by step.
DoCmd.OpenForm stDocName, , , , acFormAdd, , Me![Enquiry/Quote]
This opens the form specified by stDocName in data entry mode and passes the
value of [Enquiry/Quote] to it as a string. Now something to note here that
may or may not be important. The DoCmd methods 'simulate' the Access
actions. If the form specified by stDocName is already Open/Loaded then DoCmd
simply makes that copy visible and brings it to the front changing its data
entry mode as necessary; it does not open a new copy of the form. If the
form was Dirty then an update is triggered and this update may fail
validation. But let's assume the stDocName form was not loaded and a fresh
new copy is opened and loaded in data entry mode with no problems.
As the form opens in data entry mode all fields will be empty or show their
default values. The autonumber primary key field will show something like
'autonumber' that will immediately change to the next number in sequence the
moment anything changes on the form.
OK, what you want to do is to copy all the quotation data from a previous
quotation referenced by the value [Enquiry/Quote] passed in OpenArgs. This
reference value should be stored in the field named [Reference].
As always there is more than one way of skinning a cat. You have chosen to
retrieve this data from the database. I would first dimension a numeric
variable to hold the old key and place the value of OpenArgs in it. You
don't have to do this and could use OpenArgs directly but I just think it's
'cleaner.'
But before you do anything else you must work out how your system 'knows'
that this is a situation where it should retrieve the data. I assume the form
can be opened in other modes and can be opened in data entry mode when there
is no previous data. One way you could determine this - and only you can
tell whether this is foolproof - is to test for data entry mode and the
presence of OpenArgs. So choose the Open or Load event of your form and
encase all the data population code with an If statement.
Dim lngReference as Long
If Me.NewRecord And Len(Trim(Nz(Me.OpenArgs,""))) > 0 then
lngReference = CLng(Me.OpenArgs)
...
End If
Let's now think about what goes in place of the ellipsis above.
You want to store your reference so poke it into the reference field.
Me![Reference] = lngReference
Everything else comes from the [Sales Enquiry Register] table so define and
open a Recordset that will contains the desired data. I am giving you the
ADO solution - I'm a bit rusty on DAO but if you offer me enough reward I'll
give you that as well.
Dim rstSER as ADODB.Recordset 'Best put this with other Dims at top of code
Set rstSER = New ADODB.Recordset 'Instantiate this object
rstSER.ActiveConnection = CurrentProject.Connection 'Establish the data source
rstSER.LockType = adLockReadOnly
rstSER.Open "SELECT * FROM [Sales Enquiry Register] WHERE [Enquiry/Quote] =
" & lngReference
Me![CompanyName] = rstSER![CompanyName]
...
rstSER.Close
Set rstSER = Nothing
A few words of explanation. The fifth line above has text-wrapped; enter it
all on the same line. In the company name assignment and following use the
form control name on the left of the assignment and the table column name on
the right. Normally these are the same.
There is no check that a record has actually been returned.
RstSER.Close closes the recordset but does not destroy it. Setting the
reference to nothing means that if there are no other references to it the
memory it occupies will be reclaimed.
Enough for now.
Regards,
Rod
:
Hi Rod,
Your help is very much appreciated.
A little more information.
The original form is linked to a table called 'Sales".
If a quote is to be revised it requires a new identifying number which will
be an autonumber (feild "Enquiry/Quote").
The information from the original form is copyed to a new form.
The original idenitying number of the original form (feild "enquiry/Quote")
becomes the reference (Field "Refernece") for the new form, but it will have
a new identifying number.
The base table "Sales" has a field called reference which the forms are
linked too.
With the new form, there will be an autonumber generated and in the refernce
feild a number from the previous form.
--
Regards
Bill
:
Bill,
Taking a closer look there's something weird about the logic and use of
[Reference]. On opening the new form you place the OpenArgs value in this
control. You then overwrite this value with a DLookUp searching for a match
on Me![Enquiry/Quote].Value. What is the value of [Enquiry/Quote] at this
time? Why do you need to overwrite the value you have so carefully passed
through the open arguments? I suspect your filter should be something like
"[Reference] = " & [Reference]
But I don't think this is related to the error. [Reference] is not bound to
the primary key by any chance, is it?
Rod
:
Hi,
I have a form with a button which allows the user to create a revision of a
quote - that is a new form is opened.
The idea is that a new form opens with a new number and there is a field
that refernces the original quote form.
The button has code,
" stDocName = "Sales Enquiry Register1"
DoCmd.OpenForm stDocName, , , , acFormAdd, , Me![Enquiry/Quote]
I'm using the existing number as the opening argument to transfer other
information.
The form opens and I get a error that the field refernce cannot be given
this value
Dim dbs As DAO.Database
Set dbs = CurrentDb
Me![Reference] = Me.OpenArgs