Main Page | Report this Page
Computers Forum Index  »  Computer - Databases - MS Access  »  VBA paste append...
Page 1 of 1    

VBA paste append...

Author Message
Stinky Pete...
Posted: Wed Oct 21, 2009 12:15 pm
Guest
Hi everyone,

I'm trying to write a past append qry and as this is the first time
I've had a go at it, I think I am way off the mark.

I've got two tables, CRF_log_tbl with a unique (not primary) number
[CRF No] and the second table with 3 fields, code, change and
[required actions]. Yes, I know they have spaces in the field names ;-
( This second table has ordained and pre-defined data. The CRF No is
a text string. The "code" field will change it's number depending on
what type of change is required eg SOPs, controlled forms, equipment
etc. Right now I am just trying to get it working for the "controlled
forms" subset, which begins with 22.

When the associated CRF form is open, upon clicking a specified check
box, I'd like to populate a "tasks" table with the current CRF No and
a subset of the pre-ordained table. What I have so far is below, but
it is hanging right now on the syntax for the WHERE clause. The goal
here is to ensure end users actually complete the task table, cause
they are not doing it "manually" right now despte being trained and
reminded continually. The tasks table concept was desinged so that
the user actually had to think about what they should be entering. Oh
well.

Suggestions would be very much appreciated. This code has been
gleaned from a couple of sources I have found.

Private Sub Controlled_Form_Click()

Dim stLinkCriteria As String 'To get the current CRF No.
Dim sqlstring As String 'SQL statement.
Dim DB As DAO.Database 'Current database
Dim rst

Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("CRF_log_tbl", dbOpenDynaset)

stLinkCriteria = "[CRF No]=" & "'" & Me![CRF No] & "'"

If [controlled form] = True Then

sqlstring = "INSERT INTO CRF_tasks_tbl ( [crf no], Code, Change,
[Required Actions] ) " & _
"SELECT stlinkcriteria as [crf no], CRF_main_tasks_list_tbl.Code,
CRF_main_tasks_list_tbl.[Type of Change], CRF_main_tasks_list_tbl.
[Required Actions] " & _
"FROM CRF_main_tasks_list_tbl WHERE ((CRF_main_tasks_list_tbl.Code)
Like "22*"));

End If

End Sub
 
James A. Fortune...
Posted: Wed Oct 21, 2009 1:43 pm
Guest
On Oct 21, 8:15 am, Stinky Pete <captainm... at (no spam) hotmail.com> wrote:
Quote:
Hi everyone,

I'm trying to write a past append qry and as this is the first time
I've had a go at it, I think I am way off the mark.

I've got two tables, CRF_log_tbl with a unique (not primary) number
[CRF No] and the second table with 3 fields, code, change and
[required actions]. Yes, I know they have spaces in the field names ;-
( This second table has ordained and pre-defined data. The CRF No is
a text string. The "code" field will change it's number depending on
what type of change is required eg SOPs, controlled forms, equipment
etc. Right now I am just trying to get it working for the "controlled
forms" subset, which begins with 22.

When the associated CRF form is open, upon clicking a specified check
box, I'd like to populate a "tasks" table with the current CRF No and
a subset of the pre-ordained table. What I have so far is below, but
it is hanging right now on the syntax for the WHERE clause. The goal
here is to ensure end users actually complete the task table, cause
they are not doing it "manually" right now despte being trained and
reminded continually. The tasks table concept was desinged so that
the user actually had to think about what they should be entering. Oh
well.

Suggestions would be very much appreciated. This code has been
gleaned from a couple of sources I have found.

Private Sub Controlled_Form_Click()

Dim stLinkCriteria As String 'To get the current CRF No.
Dim sqlstring As String 'SQL statement.
Dim DB As DAO.Database 'Current database
Dim rst

Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("CRF_log_tbl", dbOpenDynaset)

stLinkCriteria = "[CRF No]=" & "'" & Me![CRF No] & "'"

If [controlled form] = True Then

sqlstring = "INSERT INTO CRF_tasks_tbl ( [crf no], Code, Change,
[Required Actions] ) " & _
"SELECT stlinkcriteria as [crf no], CRF_main_tasks_list_tbl.Code,
CRF_main_tasks_list_tbl.[Type of Change], CRF_main_tasks_list_tbl.
[Required Actions] " & _
"FROM CRF_main_tasks_list_tbl WHERE ((CRF_main_tasks_list_tbl.Code)
Like "22*"));

End If

End Sub

SP,

When placing SQL inside a string in VBA, I often find that using
single quotes in place of double quotes is required. Perhaps use a
message box to view your SQL or save the string to a file. For
example, I'm not positive that not having a space before SELECT will
work properly, but without seeing what string you're using you might
have no idea what is causing the problem. Writing the string to a
file allows you to paste your SQL into the QBF area for testing. Note
that the expressions outside the string used to place literal values
in the string can contain double quotes without causing a problem.

As a side note, much of the ISO work I'm currently involved with is as
if the auditors and specifications are saying "database. database.
database." They want "closed loops," meaning that all tasks, such as
closing purchase orders, must have a way for a manager to monitor
which ones are still open, similar to a task lisk. All the ways of
monitoring the trends of metrics shout "database." Even the emphasis
on having all corporate procedures properly documented and followed
seems, at least to me, a way of making sure that detailed
specifications will be available for the database programmer Smile.

James A. Fortune
CDMAPoster at (no spam) FortuneJames.com
 
Tom van Stiphout...
Posted: Wed Oct 21, 2009 5:23 pm
Guest
On Wed, 21 Oct 2009 05:15:13 -0700 (PDT), Stinky Pete
<captainmbag at (no spam) hotmail.com> wrote:

One very important thing that is wrong with this code is that you
likely do not have Option Explicit defined. Put it in EVERY code
module at the very top. Then find that option under Options and turn
it on for good.
Reason I know that is these two lines:
Dim DB As DAO.Database
Set dbs = CurrentDb
So you declare one variable, and use another.

Next problem:
Dim rst
That's the same as:
Dim rst as Variant
You *know* that's not the best declaration of a recordset, and in the
case of the database object you did it correctly. Why not here?
Dim rst as dao.recordset

To get to your actual question, here is your WHERE clause:
"FROM CRF_main_tasks_list_tbl WHERE ((CRF_main_tasks_list_tbl.Code)
Like "22*"));
This is incorrect string concatenation around the LIKE clause. You
can't write:
s = "aaa"bbb"));
Fortunately there is an easy workaround: use single-quotes rather than
double-quotes around the LIKE value and close the string properly:
"FROM CRF_main_tasks_list_tbl WHERE ((CRF_main_tasks_list_tbl.Code)
Like '22*'));"
If you really want to use double-quotes you have to use more than one,
and IMHO that makes the code unreadable. Plus I never know if you then
need two or three - it gets messy.

-Tom.
Microsoft Access MVP


Quote:
Hi everyone,

I'm trying to write a past append qry and as this is the first time
I've had a go at it, I think I am way off the mark.

I've got two tables, CRF_log_tbl with a unique (not primary) number
[CRF No] and the second table with 3 fields, code, change and
[required actions]. Yes, I know they have spaces in the field names ;-
( This second table has ordained and pre-defined data. The CRF No is
a text string. The "code" field will change it's number depending on
what type of change is required eg SOPs, controlled forms, equipment
etc. Right now I am just trying to get it working for the "controlled
forms" subset, which begins with 22.

When the associated CRF form is open, upon clicking a specified check
box, I'd like to populate a "tasks" table with the current CRF No and
a subset of the pre-ordained table. What I have so far is below, but
it is hanging right now on the syntax for the WHERE clause. The goal
here is to ensure end users actually complete the task table, cause
they are not doing it "manually" right now despte being trained and
reminded continually. The tasks table concept was desinged so that
the user actually had to think about what they should be entering. Oh
well.

Suggestions would be very much appreciated. This code has been
gleaned from a couple of sources I have found.

Private Sub Controlled_Form_Click()

Dim stLinkCriteria As String 'To get the current CRF No.
Dim sqlstring As String 'SQL statement.
Dim DB As DAO.Database 'Current database
Dim rst

Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("CRF_log_tbl", dbOpenDynaset)

stLinkCriteria = "[CRF No]=" & "'" & Me![CRF No] & "'"

If [controlled form] = True Then

sqlstring = "INSERT INTO CRF_tasks_tbl ( [crf no], Code, Change,
[Required Actions] ) " & _
"SELECT stlinkcriteria as [crf no], CRF_main_tasks_list_tbl.Code,
CRF_main_tasks_list_tbl.[Type of Change], CRF_main_tasks_list_tbl.
[Required Actions] " & _
"FROM CRF_main_tasks_list_tbl WHERE ((CRF_main_tasks_list_tbl.Code)
Like "22*"));

End If

End Sub
 
Stinky Pete...
Posted: Thu Oct 22, 2009 12:17 am
Guest
On Oct 21, 11:43 pm, "James A. Fortune" <CDMAPos... at (no spam) FortuneJames.com>
wrote:
Quote:
On Oct 21, 8:15 am, Stinky Pete <captainm... at (no spam) hotmail.com> wrote:



Hi everyone,

I'm trying to write a past append qry and as this is the first time
I've had a go at it, I think I am way off the mark.

I've got two tables, CRF_log_tbl with a unique (not primary) number
[CRF No] and the second table with 3 fields, code, change and
[required actions].  Yes, I know they have spaces in the field names ;-
(  This second table has ordained and pre-defined data.  The CRF No is
a text string.  The "code" field will change it's number depending on
what type of change is required eg SOPs, controlled forms, equipment
etc. Right now I am just trying to get it working for the "controlled
forms" subset, which begins with 22.

When the associated CRF form is open, upon clicking a specified check
box, I'd like to populate a "tasks" table with the current CRF No and
a subset of the pre-ordained table.  What I have so far is below, but
it is hanging right now on the syntax for the WHERE clause.  The goal
here is to ensure end users actually complete the task table, cause
they are not doing it "manually" right now despte being trained and
reminded continually.  The tasks table concept was desinged so that
the user actually had to think about what they should be entering. Oh
well.

Suggestions would be very much appreciated.  This code has been
gleaned from a couple of sources I have found.

Private Sub Controlled_Form_Click()

Dim stLinkCriteria As String        'To get the current CRF No.
Dim sqlstring As String             'SQL statement.
Dim DB As DAO.Database              'Current database
Dim rst

Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("CRF_log_tbl", dbOpenDynaset)

stLinkCriteria = "[CRF No]=" & "'" & Me![CRF No] & "'"

If [controlled form] = True Then

sqlstring = "INSERT INTO CRF_tasks_tbl ( [crf no], Code, Change,
[Required Actions] ) " & _
"SELECT stlinkcriteria as [crf no], CRF_main_tasks_list_tbl.Code,
CRF_main_tasks_list_tbl.[Type of Change], CRF_main_tasks_list_tbl.
[Required Actions] " & _
"FROM CRF_main_tasks_list_tbl WHERE ((CRF_main_tasks_list_tbl.Code)
Like "22*"));

End If

End Sub

SP,

When placing SQL inside a string in VBA, I often find that using
single quotes in place of double quotes is required.  Perhaps use a
message box to view your SQL or save the string to a file.  For
example, I'm not positive that not having a space before SELECT will
work properly, but without seeing what string you're using you might
have no idea what is causing the problem.  Writing the string to a
file allows you to paste your SQL into the QBF area for testing.  Note
that the expressions outside the string used to place literal values
in the string can contain double quotes without causing a problem.

As a side note, much of the ISO work I'm currently involved with is as
if the auditors and specifications are saying "database. database.
database."  They want "closed loops," meaning that all tasks, such as
closing purchase orders, must have a way for a manager to monitor
which ones are still open, similar to a task lisk.  All the ways of
monitoring the trends of metrics shout "database."  Even the emphasis
on having all corporate procedures properly documented and followed
seems, at least to me, a way of making sure that detailed
specifications will be available for the database programmer Smile.

James A. Fortune
CDMAPos... at (no spam) FortuneJames.com

Tom and James,

Thanx for the feeback and comments. Amending the declarations and
changing the sql string to single quotes has now enabled me to at
least compile the file OK, but for some reason, none of the fields are
populating as needed. Anyway, I'll keep plugging away and see what
happens. I'm in a tough situation, in that I am not supposed to be
amending or developing at work, and yet everyone needs the file
changed. I'll get back onto this as soon as I can.

Rgds,

Stinky Pete.
 
 
Page 1 of 1    
All times are GMT
The time now is Tue Dec 15, 2009 9:53 am