access modules in access 2000 from access 97

E

Edward Letendre

Okay, I am in the process of re-creating some modules that were used in an
access 2.0 application that was upgraded to access 97 and will eventually be
converted to an access 2000 application. The basic problems I am having are
as follows:

1. In the code the previous programmer used a function to delete or remove
a querry to remove it from memmory (I guess) if no results were returned (at
least this is my assumption as there is no one to talk to at this point).
The code used to do this is as follows:

DeleteQuerryDef('my querry')

Since this cannot be used in Access 2000 as it is written (I get conversion
errors with this code), what is there in access 2000 that does the same
thing? (If my assumptions on the use of this code is incorrect, please
correct me as I am new to access coding).

The second problem I am having is kind of two parts:

1. Within this database, there is the use of a variable called current_year
or curyear or something like that. This variable is an interger that holds
the current year (such as 2005). The problem with this coding method is that
when the year changes to 2006 and beyond, someone will have to recode the
year variable. I want to be able to get the current year from a function,
but I am not sure what will work, as I simply want to have this variable to
initialize to the current value of the year the program is run or executed or
whatever you want to call it. I have tried date() and I have though about
now(), but I am not sure how to get the date or now function to work into
displaying just the current year.

2. Once I have the current year, I need to be able to step through a
looping function to either add data to a form or add data to a table. In
either case, I need to be able to step through a loop starting with the
current year (say 2005 and work my way through the years to 1995 and along
the way, I need to load a table such as ATS_LINES_2005, ATS_LINES_2004, etc.
In each case, the name of the file is the same except for the year. Is there
a way to load the file using a variable in the name (ie: ATS_LINES_variable
for year such as ATS_LINES_2005 with the variable representing the year each
time it is executed). If so then I can get rid of the case statements as
they have to be re-created each year with new values for the year search.

Also, what other common problems might I see in converting code from access
97 to access 2000 (I have downloaded and read the access 2000 conversion
white paper and did not get the specific answers I need to my problems so
please help me if you can).

Edward Letendre.
 
K

Ken Snell [MVP]

Comments inline...

--

Ken Snell
<MS ACCESS MVP>

Edward Letendre said:
Okay, I am in the process of re-creating some modules that were used in an
access 2.0 application that was upgraded to access 97 and will eventually
be
converted to an access 2000 application. The basic problems I am having
are
as follows:

1. In the code the previous programmer used a function to delete or
remove
a querry to remove it from memmory (I guess) if no results were returned
(at
least this is my assumption as there is no one to talk to at this point).
The code used to do this is as follows:

DeleteQuerryDef('my querry')

Since this cannot be used in Access 2000 as it is written (I get
conversion
errors with this code), what is there in access 2000 that does the same
thing? (If my assumptions on the use of this code is incorrect, please
correct me as I am new to access coding).


My guess is that the previous programmer had written a custom function that
he/she named DeleteQuerryDef, and that code within that function deleted the
query. You can delete a query by using
CurrentDb.QueryDefs("QueryName").Delete



The second problem I am having is kind of two parts:

1. Within this database, there is the use of a variable called
current_year
or curyear or something like that. This variable is an interger that
holds
the current year (such as 2005). The problem with this coding method is
that
when the year changes to 2006 and beyond, someone will have to recode the
year variable. I want to be able to get the current year from a function,
but I am not sure what will work, as I simply want to have this variable
to
initialize to the current value of the year the program is run or executed
or
whatever you want to call it. I have tried date() and I have though about
now(), but I am not sure how to get the date or now function to work into
displaying just the current year.

Use the Year function:
CurrentYearNumber = Year(Date())


2. Once I have the current year, I need to be able to step through a
looping function to either add data to a form or add data to a table. In
either case, I need to be able to step through a loop starting with the
current year (say 2005 and work my way through the years to 1995 and along
the way, I need to load a table such as ATS_LINES_2005, ATS_LINES_2004,
etc.
In each case, the name of the file is the same except for the year. Is
there
a way to load the file using a variable in the name (ie:
ATS_LINES_variable
for year such as ATS_LINES_2005 with the variable representing the year
each
time it is executed). If so then I can get rid of the case statements as
they have to be re-created each year with new values for the year search.

Load a table? name of file? You'll have to provide much more details about
this. It's not clear what you're wanting to do here. Are you wanting to
import data from some type of files?
 
E

Edward Letendre

Thanks for the reply...as for your response, I added my comments below to
continue the discussion:

Ken Snell said:
Comments inline...

--

Ken Snell
<MS ACCESS MVP>




My guess is that the previous programmer had written a custom function that
he/she named DeleteQuerryDef, and that code within that function deleted the
query. You can delete a query by using
CurrentDb.QueryDefs("QueryName").Delete





Use the Year function:
CurrentYearNumber = Year(Date())

What else I need to know is how does such a function work in a table. If I
have a column defined as a date and I have the date column setup to display
the date as dd-mmm-yy and I want to convert the default output to just a date
(yyyy) only, what should I use as a default date and what format should I set
the date column as.
Load a table? name of file? You'll have to provide much more details about
this. It's not clear what you're wanting to do here. Are you wanting to
import data from some type of files?

The use of the file from above (ats_lines_2005, ats_lines_2004, etc.) was
from a section of code that used a case statement from the user's input in a
form. The user would be asked for the information that he or she is
searching for (company name and name of project and what year to search for).
As an example, the user could choose a company such as Shell and a project
such as red rose (for instance). The year would be filled in through a radio
button in the form (1995 to 2005 for choices). Once this information is
entered, the program takes the results and uses the case statement to load
the correct lines table into the program and does a search there. Thus the
case statement compares the years from 95 to 2005, and each time it loads the
file from the match with the case statement. As of an example, if the user
choose 2003 in the radio button, then the case statement 2003 would be picked
and the file ATS_LINES_2003 would be loaded into the variables and a search
would take place until a match is found. If no match is found then an error
screen pops up telling the user that their project and company info could not
be found in the line table (ATS_LINES_2003). The user would then search
again with a new year and the same company and project and a new date from
the radio button choices.

My method of trying to change this situation is to use a do while statement
that would begin in the year 2005 (or whatever the curren year is, from what
you said could be done with the year(date()) functions and begin the loop at
that year and continue searching until a match is found, counting down the
dates from 2005 until 1994. So the do while would be until 1994 is reached
or a match is found. In using this method, I need to have a file loaded into
variables, one file at a time, using the name format (ATS_LINES_year). And
in this case, it would start with ATS_LINES_2005 and would need to be changed
for each file load into the variables for the serach (ATS_LINES_2005,
ATS_LINES_2004, ATS_LINES_2003, etc.). So what I needed to know earlier is
this: Can a variable to substituted in the name call such as
ATS_LINES_year_variable, where year_variable begins at 2005 and is reduced or
subtracted by 1 for each year in the database tables from 2005, 2004, 2003,
etc. until it hits 1994 which would break out of the loop as there is no 1994
table.

The use of the case statement in this database is in a few places and I
would like to eliminate it as a programmer or simular person would have to go
back into the program each year and add to the case statements and the
current year variable as it is now.

Edward Letendre.
 
K

Ken Snell [MVP]

Answers/comments to your replies:
------------------
"What else I need to know is how does such a function work in a table. If I
have a column defined as a date and I have the date column setup to display
the date as dd-mmm-yy and I want to convert the default output to just a
date
(yyyy) only, what should I use as a default date and what format should I
set
the date column as. "

The format of a date/time field does not influence how the data are stored
in that field. It just controls how the data are displayed. Thus, if you
want a date/time field to show just the year value of the date that is
stored in that field, just set the Format property to yyyy. Alternatively,
you can use this format for a control (on a form or report) that is bound to
that field.

The expression that I'd provided in the earlier post ( CurrentYearNumber =
Year(Date()) ) calculates the year value from the current date. If you want
to do something similar with the date value from a field, just replace
Date() with the name of the field (if you're doing this in a query):
CurrentYearNumber = Year([DateTimeFieldName])
-------------------

From your description of the ATS_LINES_variable feature, it appears that you
want the ability to let the user select values that are to be used for
finding specific data, and then the program needs to query your tables to
find the appropriate data. I am still confused by your use of the term
"files" with respect to this -- do you mean tables when you say files? or do
you mean separate database files that have those names?

Could you post the code that contains the "SELECT CASE" code so that we can
see what is being done currently?
 
E

Edward Letendre

Okay, here is an example of a case statement that I wanted to you to see to
help me with this problem:


Function SearchLines(dtype As String)

Dim db As Database
Dim scrit As String
Dim MyMark As String
Dim JobForm As Form
Dim FormName As String
Dim SearchForm As Form
Dim value As Integer
Dim clientcode, Prospect As String
Dim getval As Variant
Dim prevcrit As Integer
Dim linename As String
Dim dateval As Variant
Dim jobtab As String
Dim linetab As String
Dim Querystr As String
Dim SearchQuery As QueryDef
Dim delquery As Integer
Dim I As Integer
Dim Yeartype As Integer
Dim MenuName As String


Set db = DBEngine.Workspaces(0).Databases(0)
MenuName = Screen.ActiveForm.Name
Set SearchForm = Forms(MenuName)

delquery = 0

Yeartype = SearchForm![Yearval]

'MsgBox Str$(Yeartype), 48, "Year"
Select Case Yeartype
Case 95: jobtab = "ATS JOBS 1995"
linetab = "ATS LINES 1995"
Case 96: jobtab = "ATS JOBS 1996"
linetab = "ATS LINES 1996"
Case 97: jobtab = "ATS JOBS 1997"
linetab = "ATS LINES 1997"
Case 98: jobtab = "ATS JOBS 1998"
linetab = "ATS LINES 1998"
Case 99: jobtab = "ATS JOBS 1999"
linetab = "ATS LINES 1999"
Case 2000: jobtab = "ATS JOBS 2000"
linetab = "ATS LINES 2000"
Case 2001: jobtab = "ATS JOBS 2001"
linetab = "ATS LINES 2001"
Case 2002: jobtab = "ATS JOBS 2002"
linetab = "ATS LINES 2002"
Case 2003: jobtab = "ATS JOBS 2003"
linetab = "ATS LINES 2003"
Case 2004: jobtab = "ATS JOBS 2004"
linetab = "ATS LINES 2004"
Case 2005: jobtab = "ATS JOBS 2005"
linetab = "ATS LINES 2005"
Case Else: Exit Function
End Select


prevcrit = 0
scrit = "Select lines.*, jobs.Prospect From ["
scrit = scrit & linetab & "] as lines, ["
scrit = scrit & jobtab & "] as jobs Where "

'MsgBox FORMS![VIEW JOBS]![CURRENT DATE], 48, "DATE"

'Check if client is a search criteria
If (SearchForm![CLIENT CONTROL] = True) Then
getval = SearchForm![CLIENT]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "You MUST select a Client if Client option checked!", 48, ""
Exit Function
Else
clientcode = getval
scrit = scrit & "jobs.[Client Code] = '" & clientcode & "'"
' MsgBox scrit, 48, ""
prevcrit = 1
End If
End If

If (SearchForm![PROSPECT CONTROL] = True) Then
getval = SearchForm![Prospect]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "You MUST select a Prospect if Prospect option checked!", 48, ""
Exit Function
Else
Prospect = getval
If prevcrit = 1 Then
scrit = scrit & " and "
End If
scrit = scrit & "[Prospect] like '" & Prospect & "'"
' MsgBox scrit, 48, ""
prevcrit = 1
End If
End If

If (SearchForm![LINE CONTROL] = True) Then
getval = SearchForm![LINE]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "You MUST select a Line if Line Name option checked!", 48, ""
Exit Function
Else
linename = getval
If prevcrit = 1 Then
scrit = scrit & " and "
End If
scrit = scrit & "[Line Name] like '" & linename & "'"
' MsgBox scrit, 48, ""
prevcrit = 1
End If
End If

If (SearchForm![DATE CONTROL] = True) Then
getval = SearchForm![Date]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "You MUST select a Date if Date Before option checked!", 48, ""
Exit Function
Else
dateval = CLng(getval)

If prevcrit = 1 Then
scrit = scrit & " and "
End If
scrit = scrit & "[Date Received] > " & dateval
' MsgBox scrit, 48, ""
prevcrit = 1
End If
End If

If prevcrit = 0 Then
MsgBox "NO Search Criteria were selected!", 48, "WARNING"
Exit Function
End If

scrit = scrit & " AND jobs.[Year] = lines.[Year] and jobs.[Client Code] = "
scrit = scrit & " lines.[Client Code] and jobs.[Job Code] = lines.[Job Code];"
'MsgBox scrit, 48, ""

If db.QueryDefs.Count > 0 Then
For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "LineSearch") Then
delquery = 1
End If
Next I
End If

If (delquery = 1) Then
db.DeleteQueryDef "LineSearch"
End If


'DoCmd Close A_Form, "Edit Job Info"
Set SearchQuery = db.CreateQueryDef()
SearchQuery.Name = "LineSearch"
SearchQuery.SQL = scrit

db.QueryDefs.Append SearchQuery

If (dtype = "Formview") Then
DoCmd.OpenForm "Line Search Results", A_NORMAL, , , A_READONLY
End If
If (dtype = "Sheetview") Then
DoCmd.OpenForm "Line Search Results", A_FORMDS, , , A_READONLY
End If
Forms("Line Search Results").MenuBar = "Empty_Menu"
'DoCmd Close A_FORM, "Search Lines"
DoCmd.SelectObject A_FORM, "Line Search Results"
'DoCmd RepaintObject A_Form, FormName


End Function

So this is an example of the search form code. Can you give me a full
explanation of what is happening at this point in the process. I think the
only thing missing here is the code behind the actual search which must be in
another module or function or whatever you want to call it. Help me again if
you can.

Edward Letendre.



Ken Snell said:
Answers/comments to your replies:
------------------
"What else I need to know is how does such a function work in a table. If I
have a column defined as a date and I have the date column setup to display
the date as dd-mmm-yy and I want to convert the default output to just a
date
(yyyy) only, what should I use as a default date and what format should I
set
the date column as. "

The format of a date/time field does not influence how the data are stored
in that field. It just controls how the data are displayed. Thus, if you
want a date/time field to show just the year value of the date that is
stored in that field, just set the Format property to yyyy. Alternatively,
you can use this format for a control (on a form or report) that is bound to
that field.

The expression that I'd provided in the earlier post ( CurrentYearNumber =
Year(Date()) ) calculates the year value from the current date. If you want
to do something similar with the date value from a field, just replace
Date() with the name of the field (if you're doing this in a query):
CurrentYearNumber = Year([DateTimeFieldName])
-------------------

From your description of the ATS_LINES_variable feature, it appears that you
want the ability to let the user select values that are to be used for
finding specific data, and then the program needs to query your tables to
find the appropriate data. I am still confused by your use of the term
"files" with respect to this -- do you mean tables when you say files? or do
you mean separate database files that have those names?

Could you post the code that contains the "SELECT CASE" code so that we can
see what is being done currently?



Edward Letendre said:
Thanks for the reply...as for your response, I added my comments below to
continue the discussion:



What else I need to know is how does such a function work in a table. If
I
have a column defined as a date and I have the date column setup to
display
the date as dd-mmm-yy and I want to convert the default output to just a
date
(yyyy) only, what should I use as a default date and what format should I
set
the date column as.


The use of the file from above (ats_lines_2005, ats_lines_2004, etc.) was
from a section of code that used a case statement from the user's input in
a
form. The user would be asked for the information that he or she is
searching for (company name and name of project and what year to search
for).
As an example, the user could choose a company such as Shell and a project
such as red rose (for instance). The year would be filled in through a
radio
button in the form (1995 to 2005 for choices). Once this information is
entered, the program takes the results and uses the case statement to load
the correct lines table into the program and does a search there. Thus
the
case statement compares the years from 95 to 2005, and each time it loads
the
file from the match with the case statement. As of an example, if the
user
choose 2003 in the radio button, then the case statement 2003 would be
picked
and the file ATS_LINES_2003 would be loaded into the variables and a
search
would take place until a match is found. If no match is found then an
error
screen pops up telling the user that their project and company info could
not
be found in the line table (ATS_LINES_2003). The user would then search
again with a new year and the same company and project and a new date from
the radio button choices.

My method of trying to change this situation is to use a do while
statement
that would begin in the year 2005 (or whatever the curren year is, from
what
you said could be done with the year(date()) functions and begin the loop
at
that year and continue searching until a match is found, counting down the
dates from 2005 until 1994. So the do while would be until 1994 is
reached
or a match is found. In using this method, I need to have a file loaded
into
variables, one file at a time, using the name format (ATS_LINES_year).
And
in this case, it would start with ATS_LINES_2005 and would need to be
changed
for each file load into the variables for the serach (ATS_LINES_2005,
ATS_LINES_2004, ATS_LINES_2003, etc.). So what I needed to know earlier
is
this: Can a variable to substituted in the name call such as
ATS_LINES_year_variable, where year_variable begins at 2005 and is reduced
or
subtracted by 1 for each year in the database tables from 2005, 2004,
2003,
etc. until it hits 1994 which would break out of the loop as there is no
1994
table.

The use of the case statement in this database is in a few places and I
would like to eliminate it as a programmer or simular person would have to
go
back into the program each year and add to the case statements and the
current year variable as it is now.


Edward Letendre.
 
K

Ken Snell [MVP]

Edward -

I believe what you are asking is how to replace all the CASE blocks with
simpler code.

If you wanted to use the current year as the year to add to the jobtabs and
linetab strings, you could do this:
jobtab = "ATS JOBS " & Format(Year(Date()), "yyyy")
linetab = "ATS LINES " & Format(Year(Date()), "yyyy")

It appears that you want to use the Yeartype variable, so then the steps
would be just these:
jobtab = "ATS JOBS " & IIf(Yeartype<100,"19" & Yeartype,
Yeartype)
linetab = "ATS LINES " & IIf(Yeartype<100,"19" & Yeartype,
Yeartype)

If you wanted to be sure that you'd always have the right number of digits:
jobtab = "ATS JOBS " & IIf(Yeartype<100,"19" & Format(Yeartype,
"00"), Format(Yeartype, "0000"))
linetab = "ATS LINES " & IIf(Yeartype<100,"19" & Format(Yeartype,
"00"), Format(Yeartype, "0000"))

In any of these cases, you would use just these two code steps to replace
the entire SELECT CASE block:

REPLACE these lines:
----------------------
Select Case Yeartype
Case 95: jobtab = "ATS JOBS 1995"
linetab = "ATS LINES 1995"
Case 96: jobtab = "ATS JOBS 1996"
linetab = "ATS LINES 1996"
Case 97: jobtab = "ATS JOBS 1997"
linetab = "ATS LINES 1997"
Case 98: jobtab = "ATS JOBS 1998"
linetab = "ATS LINES 1998"
Case 99: jobtab = "ATS JOBS 1999"
linetab = "ATS LINES 1999"
Case 2000: jobtab = "ATS JOBS 2000"
linetab = "ATS LINES 2000"
Case 2001: jobtab = "ATS JOBS 2001"
linetab = "ATS LINES 2001"
Case 2002: jobtab = "ATS JOBS 2002"
linetab = "ATS LINES 2002"
Case 2003: jobtab = "ATS JOBS 2003"
linetab = "ATS LINES 2003"
Case 2004: jobtab = "ATS JOBS 2004"
linetab = "ATS LINES 2004"
Case 2005: jobtab = "ATS JOBS 2005"
linetab = "ATS LINES 2005"
Case Else: Exit Function
End Select


WITH these lines (each line is one full line, so watch for word-wrapping by
the newsreader):
------------------
jobtab = "ATS JOBS " & IIf(Yeartype<100,"19" & Format(Yeartype, "00"),
Format(Yeartype, "0000"))
linetab = "ATS LINES " & IIf(Yeartype<100,"19" & Format(Yeartype, "00"),
Format(Yeartype, "0000"))



Note that the above two steps assume that the user cannot select/enter an
invalid date (that is, a year that isn't found as a table). If you need that
validation of the data entry, then you could do that by adding a few more
steps:
------------------
jobtabs = "ATS JOBS " & IIf(Yeartype<100,"19" & Format(Yeartype, "00"),
Format(Yeartype, "0000"))
linetab = "ATS LINES " & IIf(Yeartype<100,"19" & Format(Yeartype, "00"),
Format(Yeartype, "0000"))
Dim strTest As String
On Error Resume Next
strTest = CurrentDb.TableDefs(jobtab).Name
If Err.Number <> 0 Then
MsgBox "Entered year is invalid!"
Err.Clear
Exit Function
End If
strTest = CurrentDb.TableDefs(linetab).Name
If Err.Number <> 0 Then
MsgBox "Entered year is invalid!"
Err.Clear
Exit Function
End If
On Error GoTo 0


--

Ken Snell
<MS ACCESS MVP>

Edward Letendre said:
Okay, here is an example of a case statement that I wanted to you to see
to
help me with this problem:


Function SearchLines(dtype As String)

Dim db As Database
Dim scrit As String
Dim MyMark As String
Dim JobForm As Form
Dim FormName As String
Dim SearchForm As Form
Dim value As Integer
Dim clientcode, Prospect As String
Dim getval As Variant
Dim prevcrit As Integer
Dim linename As String
Dim dateval As Variant
Dim jobtab As String
Dim linetab As String
Dim Querystr As String
Dim SearchQuery As QueryDef
Dim delquery As Integer
Dim I As Integer
Dim Yeartype As Integer
Dim MenuName As String


Set db = DBEngine.Workspaces(0).Databases(0)
MenuName = Screen.ActiveForm.Name
Set SearchForm = Forms(MenuName)

delquery = 0

Yeartype = SearchForm![Yearval]

'MsgBox Str$(Yeartype), 48, "Year"
Select Case Yeartype
Case 95: jobtab = "ATS JOBS 1995"
linetab = "ATS LINES 1995"
Case 96: jobtab = "ATS JOBS 1996"
linetab = "ATS LINES 1996"
Case 97: jobtab = "ATS JOBS 1997"
linetab = "ATS LINES 1997"
Case 98: jobtab = "ATS JOBS 1998"
linetab = "ATS LINES 1998"
Case 99: jobtab = "ATS JOBS 1999"
linetab = "ATS LINES 1999"
Case 2000: jobtab = "ATS JOBS 2000"
linetab = "ATS LINES 2000"
Case 2001: jobtab = "ATS JOBS 2001"
linetab = "ATS LINES 2001"
Case 2002: jobtab = "ATS JOBS 2002"
linetab = "ATS LINES 2002"
Case 2003: jobtab = "ATS JOBS 2003"
linetab = "ATS LINES 2003"
Case 2004: jobtab = "ATS JOBS 2004"
linetab = "ATS LINES 2004"
Case 2005: jobtab = "ATS JOBS 2005"
linetab = "ATS LINES 2005"
Case Else: Exit Function
End Select


prevcrit = 0
scrit = "Select lines.*, jobs.Prospect From ["
scrit = scrit & linetab & "] as lines, ["
scrit = scrit & jobtab & "] as jobs Where "

'MsgBox FORMS![VIEW JOBS]![CURRENT DATE], 48, "DATE"

'Check if client is a search criteria
If (SearchForm![CLIENT CONTROL] = True) Then
getval = SearchForm![CLIENT]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "You MUST select a Client if Client option checked!", 48, ""
Exit Function
Else
clientcode = getval
scrit = scrit & "jobs.[Client Code] = '" & clientcode & "'"
' MsgBox scrit, 48, ""
prevcrit = 1
End If
End If

If (SearchForm![PROSPECT CONTROL] = True) Then
getval = SearchForm![Prospect]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "You MUST select a Prospect if Prospect option checked!", 48, ""
Exit Function
Else
Prospect = getval
If prevcrit = 1 Then
scrit = scrit & " and "
End If
scrit = scrit & "[Prospect] like '" & Prospect & "'"
' MsgBox scrit, 48, ""
prevcrit = 1
End If
End If

If (SearchForm![LINE CONTROL] = True) Then
getval = SearchForm![LINE]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "You MUST select a Line if Line Name option checked!", 48, ""
Exit Function
Else
linename = getval
If prevcrit = 1 Then
scrit = scrit & " and "
End If
scrit = scrit & "[Line Name] like '" & linename & "'"
' MsgBox scrit, 48, ""
prevcrit = 1
End If
End If

If (SearchForm![DATE CONTROL] = True) Then
getval = SearchForm![Date]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "You MUST select a Date if Date Before option checked!", 48, ""
Exit Function
Else
dateval = CLng(getval)

If prevcrit = 1 Then
scrit = scrit & " and "
End If
scrit = scrit & "[Date Received] > " & dateval
' MsgBox scrit, 48, ""
prevcrit = 1
End If
End If

If prevcrit = 0 Then
MsgBox "NO Search Criteria were selected!", 48, "WARNING"
Exit Function
End If

scrit = scrit & " AND jobs.[Year] = lines.[Year] and jobs.[Client Code] =
"
scrit = scrit & " lines.[Client Code] and jobs.[Job Code] = lines.[Job
Code];"
'MsgBox scrit, 48, ""

If db.QueryDefs.Count > 0 Then
For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "LineSearch") Then
delquery = 1
End If
Next I
End If

If (delquery = 1) Then
db.DeleteQueryDef "LineSearch"
End If


'DoCmd Close A_Form, "Edit Job Info"
Set SearchQuery = db.CreateQueryDef()
SearchQuery.Name = "LineSearch"
SearchQuery.SQL = scrit

db.QueryDefs.Append SearchQuery

If (dtype = "Formview") Then
DoCmd.OpenForm "Line Search Results", A_NORMAL, , , A_READONLY
End If
If (dtype = "Sheetview") Then
DoCmd.OpenForm "Line Search Results", A_FORMDS, , , A_READONLY
End If
Forms("Line Search Results").MenuBar = "Empty_Menu"
'DoCmd Close A_FORM, "Search Lines"
DoCmd.SelectObject A_FORM, "Line Search Results"
'DoCmd RepaintObject A_Form, FormName


End Function

So this is an example of the search form code. Can you give me a full
explanation of what is happening at this point in the process. I think
the
only thing missing here is the code behind the actual search which must be
in
another module or function or whatever you want to call it. Help me again
if
you can.

Edward Letendre.



Ken Snell said:
Answers/comments to your replies:
------------------
"What else I need to know is how does such a function work in a table.
If I
have a column defined as a date and I have the date column setup to
display
the date as dd-mmm-yy and I want to convert the default output to just a
date
(yyyy) only, what should I use as a default date and what format should I
set
the date column as. "

The format of a date/time field does not influence how the data are
stored
in that field. It just controls how the data are displayed. Thus, if you
want a date/time field to show just the year value of the date that is
stored in that field, just set the Format property to yyyy.
Alternatively,
you can use this format for a control (on a form or report) that is bound
to
that field.

The expression that I'd provided in the earlier post ( CurrentYearNumber
=
Year(Date()) ) calculates the year value from the current date. If you
want
to do something similar with the date value from a field, just replace
Date() with the name of the field (if you're doing this in a query):
CurrentYearNumber = Year([DateTimeFieldName])
-------------------

From your description of the ATS_LINES_variable feature, it appears that
you
want the ability to let the user select values that are to be used for
finding specific data, and then the program needs to query your tables to
find the appropriate data. I am still confused by your use of the term
"files" with respect to this -- do you mean tables when you say files? or
do
you mean separate database files that have those names?

Could you post the code that contains the "SELECT CASE" code so that we
can
see what is being done currently?



Edward Letendre said:
Thanks for the reply...as for your response, I added my comments below
to
continue the discussion:

:

Comments inline...

--

Ken Snell
<MS ACCESS MVP>

message Okay, I am in the process of re-creating some modules that were used
in
an
access 2.0 application that was upgraded to access 97 and will
eventually
be
converted to an access 2000 application. The basic problems I am
having
are
as follows:

1. In the code the previous programmer used a function to delete or
remove
a querry to remove it from memmory (I guess) if no results were
returned
(at
least this is my assumption as there is no one to talk to at this
point).
The code used to do this is as follows:

DeleteQuerryDef('my querry')

Since this cannot be used in Access 2000 as it is written (I get
conversion
errors with this code), what is there in access 2000 that does the
same
thing? (If my assumptions on the use of this code is incorrect,
please
correct me as I am new to access coding).


My guess is that the previous programmer had written a custom function
that
he/she named DeleteQuerryDef, and that code within that function
deleted
the
query. You can delete a query by using
CurrentDb.QueryDefs("QueryName").Delete





The second problem I am having is kind of two parts:

1. Within this database, there is the use of a variable called
current_year
or curyear or something like that. This variable is an interger
that
holds
the current year (such as 2005). The problem with this coding
method
is
that
when the year changes to 2006 and beyond, someone will have to
recode
the
year variable. I want to be able to get the current year from a
function,
but I am not sure what will work, as I simply want to have this
variable
to
initialize to the current value of the year the program is run or
executed
or
whatever you want to call it. I have tried date() and I have though
about
now(), but I am not sure how to get the date or now function to work
into
displaying just the current year.

Use the Year function:
CurrentYearNumber = Year(Date())


What else I need to know is how does such a function work in a table.
If
I
have a column defined as a date and I have the date column setup to
display
the date as dd-mmm-yy and I want to convert the default output to just
a
date
(yyyy) only, what should I use as a default date and what format should
I
set
the date column as.




2. Once I have the current year, I need to be able to step through
a
looping function to either add data to a form or add data to a
table.
In
either case, I need to be able to step through a loop starting with
the
current year (say 2005 and work my way through the years to 1995 and
along
the way, I need to load a table such as ATS_LINES_2005,
ATS_LINES_2004,
etc.
In each case, the name of the file is the same except for the year.
Is
there
a way to load the file using a variable in the name (ie:
ATS_LINES_variable
for year such as ATS_LINES_2005 with the variable representing the
year
each
time it is executed). If so then I can get rid of the case
statements
as
they have to be re-created each year with new values for the year
search.

Load a table? name of file? You'll have to provide much more details
about
this. It's not clear what you're wanting to do here. Are you wanting
to
import data from some type of files?


The use of the file from above (ats_lines_2005, ats_lines_2004, etc.)
was
from a section of code that used a case statement from the user's input
in
a
form. The user would be asked for the information that he or she is
searching for (company name and name of project and what year to search
for).
As an example, the user could choose a company such as Shell and a
project
such as red rose (for instance). The year would be filled in through a
radio
button in the form (1995 to 2005 for choices). Once this information
is
entered, the program takes the results and uses the case statement to
load
the correct lines table into the program and does a search there. Thus
the
case statement compares the years from 95 to 2005, and each time it
loads
the
file from the match with the case statement. As of an example, if the
user
choose 2003 in the radio button, then the case statement 2003 would be
picked
and the file ATS_LINES_2003 would be loaded into the variables and a
search
would take place until a match is found. If no match is found then an
error
screen pops up telling the user that their project and company info
could
not
be found in the line table (ATS_LINES_2003). The user would then
search
again with a new year and the same company and project and a new date
from
the radio button choices.

My method of trying to change this situation is to use a do while
statement
that would begin in the year 2005 (or whatever the curren year is, from
what
you said could be done with the year(date()) functions and begin the
loop
at
that year and continue searching until a match is found, counting down
the
dates from 2005 until 1994. So the do while would be until 1994 is
reached
or a match is found. In using this method, I need to have a file
loaded
into
variables, one file at a time, using the name format (ATS_LINES_year).
And
in this case, it would start with ATS_LINES_2005 and would need to be
changed
for each file load into the variables for the serach (ATS_LINES_2005,
ATS_LINES_2004, ATS_LINES_2003, etc.). So what I needed to know
earlier
is
this: Can a variable to substituted in the name call such as
ATS_LINES_year_variable, where year_variable begins at 2005 and is
reduced
or
subtracted by 1 for each year in the database tables from 2005, 2004,
2003,
etc. until it hits 1994 which would break out of the loop as there is
no
1994
table.

The use of the case statement in this database is in a few places and I
would like to eliminate it as a programmer or simular person would have
to
go
back into the program each year and add to the case statements and the
current year variable as it is now.




Also, what other common problems might I see in converting code from
access
97 to access 2000 (I have downloaded and read the access 2000
conversion
white paper and did not get the specific answers I need to my
problems
so
please help me if you can).

Edward Letendre.





Edward Letendre.
 
E

Edward Letendre

Thanks for the reply, I am adding some new stuff based on the info you gave
to me last. Here is an example of an loop with an if statement that I will
add to the code as a test to see if it will work, but could you please
comment on my code and make any corrections you see fit:

Dim db As Database
Dim FormName As String, SubFormName
Dim eol As Single, bol As Single
Dim statint As Single, kmcount As Single
Dim getval As Variant
Dim fm As String
Dim yearstr As String
Dim Yearval As Integer
Dim Yearcnt As Integer

Set db = DBEngine.Workspaces(0).Databases(0)


FormName = Screen.ActiveForm.Name


getval = Forms(FormName)![Year]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "No Year!", 48, ""
Exit Function
Else
yearstr = getval
End If

Yearval = Val(yearstr)

Yearcnt = 2005 'this code sets the year count variable to the year 2005

' The loop below should go from 2005 to 1994, year by year until a match is
found
in the Yearval variable

Do While Yearcnt <> 1994
If Yearval = Yearcnt Then
SubFormName = "ATS LINES " & Yearcnt 'this should add a value for
the sub form name

Else
Yearcnt = Yearcnt - 1
Loop

Here is the original case statement, that loads the sub form name variable
based on the yearval variable that I am attempting to duplicate from the
code above:

Select Case Yearval
Case 95: SubFormName = "ATS LINES 1995"
Case 96: SubFormName = "ATS LINES 1996"
Case 97: SubFormName = "ATS LINES 1997"
Case 98: SubFormName = "ATS LINES 1998"
Case 99: SubFormName = "ATS LINES 1999"
Case 2000: SubFormName = "ATS LINES 2000"
Case 2001: SubFormName = "ATS LINES 2001"
Case 2002: SubFormName = "ATS LINES 2002"
Case 2003: SubFormName = "ATS LINES 2003"
Case 2004: SubFormName = "ATS LINES 2004"
Case 2005: SubFormName = "ATS LINES 2005"
End Select

The other thing is this is some code I came across in looking at this
project, can you please explain what it means:

If db.QueryDefs.Count > 0 Then
For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "AddInv") Then
delquery = 1
End If
Next I
End If

If (delquery = 1) Then
db.DeleteQueryDef "AddInv"
End If

My confusion is with the statement: If (db.QueryDefs(I).Name = "AddInv")
Then

Why was the db.QueryDefs(I).Name used in the above code and then
the delete query request was used as follows:

db.DeleteQueryDEf "AddInv" use, where in other code I have seen the
following used as well

db.DeleteQueryDef(I).Name"AddInv".

where as in access 2000, you have to delete the query using the followng code:

CurrentDb.QueryDefs("AddInv").Delete

but you cannot delete a query this way:

CurrentDb.QueryDef(I).Name("AddInv").Delete

Thus why use the QueryDef(I).Name part of the function such as with access
97 but not in access 2000 or however it would be coded in access 2000 as
compared to access 97. I understand this is confusing but as I say how or
why do the programmers add the (I).Name part to the querydef and then delete
the query def if no occurance is not fount with the (I).Name added to it?

Edward Letendre.

Ken Snell said:
Edward -

I believe what you are asking is how to replace all the CASE blocks with
simpler code.

If you wanted to use the current year as the year to add to the jobtabs and
linetab strings, you could do this:
jobtab = "ATS JOBS " & Format(Year(Date()), "yyyy")
linetab = "ATS LINES " & Format(Year(Date()), "yyyy")

It appears that you want to use the Yeartype variable, so then the steps
would be just these:
jobtab = "ATS JOBS " & IIf(Yeartype<100,"19" & Yeartype,
Yeartype)
linetab = "ATS LINES " & IIf(Yeartype<100,"19" & Yeartype,
Yeartype)

If you wanted to be sure that you'd always have the right number of digits:
jobtab = "ATS JOBS " & IIf(Yeartype<100,"19" & Format(Yeartype,
"00"), Format(Yeartype, "0000"))
linetab = "ATS LINES " & IIf(Yeartype<100,"19" & Format(Yeartype,
"00"), Format(Yeartype, "0000"))

In any of these cases, you would use just these two code steps to replace
the entire SELECT CASE block:

REPLACE these lines:
----------------------
Select Case Yeartype
Case 95: jobtab = "ATS JOBS 1995"
linetab = "ATS LINES 1995"
Case 96: jobtab = "ATS JOBS 1996"
linetab = "ATS LINES 1996"
Case 97: jobtab = "ATS JOBS 1997"
linetab = "ATS LINES 1997"
Case 98: jobtab = "ATS JOBS 1998"
linetab = "ATS LINES 1998"
Case 99: jobtab = "ATS JOBS 1999"
linetab = "ATS LINES 1999"
Case 2000: jobtab = "ATS JOBS 2000"
linetab = "ATS LINES 2000"
Case 2001: jobtab = "ATS JOBS 2001"
linetab = "ATS LINES 2001"
Case 2002: jobtab = "ATS JOBS 2002"
linetab = "ATS LINES 2002"
Case 2003: jobtab = "ATS JOBS 2003"
linetab = "ATS LINES 2003"
Case 2004: jobtab = "ATS JOBS 2004"
linetab = "ATS LINES 2004"
Case 2005: jobtab = "ATS JOBS 2005"
linetab = "ATS LINES 2005"
Case Else: Exit Function
End Select


WITH these lines (each line is one full line, so watch for word-wrapping by
the newsreader):
------------------
jobtab = "ATS JOBS " & IIf(Yeartype<100,"19" & Format(Yeartype, "00"),
Format(Yeartype, "0000"))
linetab = "ATS LINES " & IIf(Yeartype<100,"19" & Format(Yeartype, "00"),
Format(Yeartype, "0000"))



Note that the above two steps assume that the user cannot select/enter an
invalid date (that is, a year that isn't found as a table). If you need that
validation of the data entry, then you could do that by adding a few more
steps:
------------------
jobtabs = "ATS JOBS " & IIf(Yeartype<100,"19" & Format(Yeartype, "00"),
Format(Yeartype, "0000"))
linetab = "ATS LINES " & IIf(Yeartype<100,"19" & Format(Yeartype, "00"),
Format(Yeartype, "0000"))
Dim strTest As String
On Error Resume Next
strTest = CurrentDb.TableDefs(jobtab).Name
If Err.Number <> 0 Then
MsgBox "Entered year is invalid!"
Err.Clear
Exit Function
End If
strTest = CurrentDb.TableDefs(linetab).Name
If Err.Number <> 0 Then
MsgBox "Entered year is invalid!"
Err.Clear
Exit Function
End If
On Error GoTo 0


--

Ken Snell
<MS ACCESS MVP>

Edward Letendre said:
Okay, here is an example of a case statement that I wanted to you to see
to
help me with this problem:


Function SearchLines(dtype As String)

Dim db As Database
Dim scrit As String
Dim MyMark As String
Dim JobForm As Form
Dim FormName As String
Dim SearchForm As Form
Dim value As Integer
Dim clientcode, Prospect As String
Dim getval As Variant
Dim prevcrit As Integer
Dim linename As String
Dim dateval As Variant
Dim jobtab As String
Dim linetab As String
Dim Querystr As String
Dim SearchQuery As QueryDef
Dim delquery As Integer
Dim I As Integer
Dim Yeartype As Integer
Dim MenuName As String


Set db = DBEngine.Workspaces(0).Databases(0)
MenuName = Screen.ActiveForm.Name
Set SearchForm = Forms(MenuName)

delquery = 0

Yeartype = SearchForm![Yearval]

'MsgBox Str$(Yeartype), 48, "Year"
Select Case Yeartype
Case 95: jobtab = "ATS JOBS 1995"
linetab = "ATS LINES 1995"
Case 96: jobtab = "ATS JOBS 1996"
linetab = "ATS LINES 1996"
Case 97: jobtab = "ATS JOBS 1997"
linetab = "ATS LINES 1997"
Case 98: jobtab = "ATS JOBS 1998"
linetab = "ATS LINES 1998"
Case 99: jobtab = "ATS JOBS 1999"
linetab = "ATS LINES 1999"
Case 2000: jobtab = "ATS JOBS 2000"
linetab = "ATS LINES 2000"
Case 2001: jobtab = "ATS JOBS 2001"
linetab = "ATS LINES 2001"
Case 2002: jobtab = "ATS JOBS 2002"
linetab = "ATS LINES 2002"
Case 2003: jobtab = "ATS JOBS 2003"
linetab = "ATS LINES 2003"
Case 2004: jobtab = "ATS JOBS 2004"
linetab = "ATS LINES 2004"
Case 2005: jobtab = "ATS JOBS 2005"
linetab = "ATS LINES 2005"
Case Else: Exit Function
End Select


prevcrit = 0
scrit = "Select lines.*, jobs.Prospect From ["
scrit = scrit & linetab & "] as lines, ["
scrit = scrit & jobtab & "] as jobs Where "

'MsgBox FORMS![VIEW JOBS]![CURRENT DATE], 48, "DATE"

'Check if client is a search criteria
If (SearchForm![CLIENT CONTROL] = True) Then
getval = SearchForm![CLIENT]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "You MUST select a Client if Client option checked!", 48, ""
Exit Function
Else
clientcode = getval
scrit = scrit & "jobs.[Client Code] = '" & clientcode & "'"
' MsgBox scrit, 48, ""
prevcrit = 1
End If
End If

If (SearchForm![PROSPECT CONTROL] = True) Then
getval = SearchForm![Prospect]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "You MUST select a Prospect if Prospect option checked!", 48, ""
Exit Function
Else
Prospect = getval
If prevcrit = 1 Then
scrit = scrit & " and "
End If
scrit = scrit & "[Prospect] like '" & Prospect & "'"
' MsgBox scrit, 48, ""
prevcrit = 1
End If
End If

If (SearchForm![LINE CONTROL] = True) Then
getval = SearchForm![LINE]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "You MUST select a Line if Line Name option checked!", 48, ""
Exit Function
Else
linename = getval
If prevcrit = 1 Then
scrit = scrit & " and "
End If
scrit = scrit & "[Line Name] like '" & linename & "'"
' MsgBox scrit, 48, ""
prevcrit = 1
End If
End If

If (SearchForm![DATE CONTROL] = True) Then
getval = SearchForm![Date]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "You MUST select a Date if Date Before option checked!", 48, ""
Exit Function
Else
dateval = CLng(getval)

If prevcrit = 1 Then
scrit = scrit & " and "
End If
scrit = scrit & "[Date Received] > " & dateval
' MsgBox scrit, 48, ""
prevcrit = 1
End If
End If

If prevcrit = 0 Then
MsgBox "NO Search Criteria were selected!", 48, "WARNING"
Exit Function
End If

scrit = scrit & " AND jobs.[Year] = lines.[Year] and jobs.[Client Code] =
"
scrit = scrit & " lines.[Client Code] and jobs.[Job Code] = lines.[Job
Code];"
'MsgBox scrit, 48, ""

If db.QueryDefs.Count > 0 Then
For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "LineSearch") Then
delquery = 1
End If
Next I
End If

If (delquery = 1) Then
db.DeleteQueryDef "LineSearch"
End If


'DoCmd Close A_Form, "Edit Job Info"
Set SearchQuery = db.CreateQueryDef()
SearchQuery.Name = "LineSearch"
SearchQuery.SQL = scrit

db.QueryDefs.Append SearchQuery

If (dtype = "Formview") Then
DoCmd.OpenForm "Line Search Results", A_NORMAL, , , A_READONLY
End If
If (dtype = "Sheetview") Then
DoCmd.OpenForm "Line Search Results", A_FORMDS, , , A_READONLY
End If
Forms("Line Search Results").MenuBar = "Empty_Menu"
'DoCmd Close A_FORM, "Search Lines"
DoCmd.SelectObject A_FORM, "Line Search Results"
'DoCmd RepaintObject A_Form, FormName


End Function

So this is an example of the search form code. Can you give me a full
explanation of what is happening at this point in the process. I think
the
only thing missing here is the code behind the actual search which must be
in
another module or function or whatever you want to call it. Help me again
if
you can.

Edward Letendre.



Ken Snell said:
Answers/comments to your replies:
------------------
"What else I need to know is how does such a function work in a table.
If I
have a column defined as a date and I have the date column setup to
display
the date as dd-mmm-yy and I want to convert the default output to just a
date
(yyyy) only, what should I use as a default date and what format should I
set
the date column as. "

The format of a date/time field does not influence how the data are
stored
in that field. It just controls how the data are displayed. Thus, if you
want a date/time field to show just the year value of the date that is
stored in that field, just set the Format property to yyyy.
Alternatively,
you can use this format for a control (on a form or report) that is bound
to
that field.

The expression that I'd provided in the earlier post ( CurrentYearNumber
=
Year(Date()) ) calculates the year value from the current date. If you
want
to do something similar with the date value from a field, just replace
Date() with the name of the field (if you're doing this in a query):
CurrentYearNumber = Year([DateTimeFieldName])
-------------------

From your description of the ATS_LINES_variable feature, it appears that
you
want the ability to let the user select values that are to be used for
finding specific data, and then the program needs to query your tables to
find the appropriate data. I am still confused by your use of the term
"files" with respect to this -- do you mean tables when you say files? or
do
you mean separate database files that have those names?

Could you post the code that contains the "SELECT CASE" code so that we
can
see what is being done currently?



message Thanks for the reply...as for your response, I added my comments below
to
continue the discussion:

:

Comments inline...

--

Ken Snell
<MS ACCESS MVP>

message Okay, I am in the process of re-creating some modules that were used
in
an
access 2.0 application that was upgraded to access 97 and will
eventually
be
converted to an access 2000 application. The basic problems I am
having
are
as follows:

1. In the code the previous programmer used a function to delete or
remove
a querry to remove it from memmory (I guess) if no results were
returned
(at
least this is my assumption as there is no one to talk to at this
point).
The code used to do this is as follows:

DeleteQuerryDef('my querry')

Since this cannot be used in Access 2000 as it is written (I get
conversion
errors with this code), what is there in access 2000 that does the
same
thing? (If my assumptions on the use of this code is incorrect,
please
correct me as I am new to access coding).


My guess is that the previous programmer had written a custom function
that
he/she named DeleteQuerryDef, and that code within that function
deleted
the
query. You can delete a query by using
CurrentDb.QueryDefs("QueryName").Delete





The second problem I am having is kind of two parts:

1. Within this database, there is the use of a variable called
current_year
or curyear or something like that. This variable is an interger
that
holds
the current year (such as 2005). The problem with this coding
method
is
that
when the year changes to 2006 and beyond, someone will have to
recode
the
year variable. I want to be able to get the current year from a
function,
but I am not sure what will work, as I simply want to have this
variable
to
initialize to the current value of the year the program is run or
executed
or
whatever you want to call it. I have tried date() and I have though
about
now(), but I am not sure how to get the date or now function to work
into
displaying just the current year.

Use the Year function:
CurrentYearNumber = Year(Date())


What else I need to know is how does such a function work in a table.
If
I
have a column defined as a date and I have the date column setup to
display
the date as dd-mmm-yy and I want to convert the default output to just
a
date
(yyyy) only, what should I use as a default date and what format should
I
set
the date column as.




2. Once I have the current year, I need to be able to step through
a
looping function to either add data to a form or add data to a
table.
In
either case, I need to be able to step through a loop starting with
the
current year (say 2005 and work my way through the years to 1995 and
along
the way, I need to load a table such as ATS_LINES_2005,
ATS_LINES_2004,
etc.
In each case, the name of the file is the same except for the year.
Is
there
a way to load the file using a variable in the name (ie:
ATS_LINES_variable
for year such as ATS_LINES_2005 with the variable representing the
year
each
time it is executed). If so then I can get rid of the case
statements
as
they have to be re-created each year with new values for the year
search.

Load a table? name of file? You'll have to provide much more details
about
this. It's not clear what you're wanting to do here. Are you wanting
to
import data from some type of files?


The use of the file from above (ats_lines_2005, ats_lines_2004, etc.)
was
from a section of code that used a case statement from the user's input
in
a
form. The user would be asked for the information that he or she is
searching for (company name and name of project and what year to search
for).
As an example, the user could choose a company such as Shell and a
project
such as red rose (for instance). The year would be filled in through a
radio
button in the form (1995 to 2005 for choices). Once this information
is
entered, the program takes the results and uses the case statement to
load
the correct lines table into the program and does a search there. Thus
the
case statement compares the years from 95 to 2005, and each time it
loads
the
file from the match with the case statement. As of an example, if the
user
choose 2003 in the radio button, then the case statement 2003 would be
picked
and the file ATS_LINES_2003 would be loaded into the variables and a
search
would take place until a match is found. If no match is found then an
error
screen pops up telling the user that their project and company info
could
not
be found in the line table (ATS_LINES_2003). The user would then
search
again with a new year and the same company and project and a new date
from
the radio button choices.

My method of trying to change this situation is to use a do while
statement
that would begin in the year 2005 (or whatever the curren year is, from
what
you said could be done with the year(date()) functions and begin the
loop
at
that year and continue searching until a match is found, counting down
the
dates from 2005 until 1994. So the do while would be until 1994 is
reached
or a match is found. In using this method, I need to have a file
loaded
into
variables, one file at a time, using the name format (ATS_LINES_year).
And
in this case, it would start with ATS_LINES_2005 and would need to be
changed
for each file load into the variables for the serach (ATS_LINES_2005,
ATS_LINES_2004, ATS_LINES_2003, etc.). So what I needed to know
earlier
is
this: Can a variable to substituted in the name call such as
ATS_LINES_year_variable, where year_variable begins at 2005 and is
reduced
or
subtracted by 1 for each year in the database tables from 2005, 2004,
2003,
etc. until it hits 1994 which would break out of the loop as there is
no
1994
table.

The use of the case statement in this database is in a few places and I
would like to eliminate it as a programmer or simular person would have
to
go
back into the program each year and add to the case statements and the
current year variable as it is now.




Also, what other common problems might I see in converting code from
access
97 to access 2000 (I have downloaded and read the access 2000
conversion
white paper and did not get the specific answers I need to my
problems
so
please help me if you can).

Edward Letendre.





Edward Letendre.
 
K

Ken Snell [MVP]

I would change this line
Set db = DBEngine.Workspaces(0).Databases(0)
to this
Set db = CurrentDb

Your original code used 95, 96, 97, 98, and 99 for the years 1995, 1996,
etc., but the code here will expect to see the full 1995 numbers, not the 95
that was in the original code. Not sure if that will meet your needs.

This code:
Do While Yearcnt <> 1994
If Yearval = Yearcnt Then
SubFormName = "ATS LINES " & Yearcnt 'this should add a value
for the sub form name

Else
Yearcnt = Yearcnt - 1
Loop

will never get out of the loop. You don't decrement the yearcnt value when
it matches the Yearval value, so the loop will be endless. It should be
something like this:
Do While Yearcnt <> 1994
If Yearval = Yearcnt Then
SubFormName = "ATS LINES " & Yearcnt 'this should add a value
for the sub form name
Exit Do
Else
Yearcnt = Yearcnt - 1
Loop


The QueryDefs loop is looking to see if your database contains a query named
AddInv. If it's found, the database deletes it from the database. This code
is a bit of a round-a-bout way to do that, but it'll work.

--

Ken Snell
<MS ACCESS MVP>




Edward Letendre said:
Thanks for the reply, I am adding some new stuff based on the info you
gave
to me last. Here is an example of an loop with an if statement that I
will
add to the code as a test to see if it will work, but could you please
comment on my code and make any corrections you see fit:

Dim db As Database
Dim FormName As String, SubFormName
Dim eol As Single, bol As Single
Dim statint As Single, kmcount As Single
Dim getval As Variant
Dim fm As String
Dim yearstr As String
Dim Yearval As Integer
Dim Yearcnt As Integer

Set db = DBEngine.Workspaces(0).Databases(0)


FormName = Screen.ActiveForm.Name


getval = Forms(FormName)![Year]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "No Year!", 48, ""
Exit Function
Else
yearstr = getval
End If

Yearval = Val(yearstr)

Yearcnt = 2005 'this code sets the year count variable to the year 2005

' The loop below should go from 2005 to 1994, year by year until a match
is
found
in the Yearval variable

Do While Yearcnt <> 1994
If Yearval = Yearcnt Then
SubFormName = "ATS LINES " & Yearcnt 'this should add a value for
the sub form name

Else
Yearcnt = Yearcnt - 1
Loop

Here is the original case statement, that loads the sub form name variable
based on the yearval variable that I am attempting to duplicate from the
code above:

Select Case Yearval
Case 95: SubFormName = "ATS LINES 1995"
Case 96: SubFormName = "ATS LINES 1996"
Case 97: SubFormName = "ATS LINES 1997"
Case 98: SubFormName = "ATS LINES 1998"
Case 99: SubFormName = "ATS LINES 1999"
Case 2000: SubFormName = "ATS LINES 2000"
Case 2001: SubFormName = "ATS LINES 2001"
Case 2002: SubFormName = "ATS LINES 2002"
Case 2003: SubFormName = "ATS LINES 2003"
Case 2004: SubFormName = "ATS LINES 2004"
Case 2005: SubFormName = "ATS LINES 2005"
End Select

The other thing is this is some code I came across in looking at this
project, can you please explain what it means:

If db.QueryDefs.Count > 0 Then
For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "AddInv") Then
delquery = 1
End If
Next I
End If

If (delquery = 1) Then
db.DeleteQueryDef "AddInv"
End If

My confusion is with the statement: If (db.QueryDefs(I).Name = "AddInv")
Then

Why was the db.QueryDefs(I).Name used in the above code and then
the delete query request was used as follows:

db.DeleteQueryDEf "AddInv" use, where in other code I have seen the
following used as well

db.DeleteQueryDef(I).Name"AddInv".

where as in access 2000, you have to delete the query using the followng
code:

CurrentDb.QueryDefs("AddInv").Delete

but you cannot delete a query this way:

CurrentDb.QueryDef(I).Name("AddInv").Delete

Thus why use the QueryDef(I).Name part of the function such as with access
97 but not in access 2000 or however it would be coded in access 2000 as
compared to access 97. I understand this is confusing but as I say how or
why do the programmers add the (I).Name part to the querydef and then
delete
the query def if no occurance is not fount with the (I).Name added to it?

Edward Letendre.
 
E

Edward Letendre

Thanks for the information up to this point. I am really learning a lot and
access 2000 code is looking much like Oracle stuff with functions, code, etc.
in a database standard way. Anyway, I asked this earlier, but here is full
example of some code that was used in this application. I see that a
variable related to the querydef type of variable was used in this function
or sub-routine or whatever you want to call it,a s it is in the module
section of Access 97/2000. The use of the variable is also through this
particular section of code and I believe that a querydef is used to access a
query of a table or a table structure (rows, columns, etc.). So in going
from Access 97 to Access 2000, is this code sound (I know that the way to
delete a query, as you mentioned before is to use CurrentDb.QueryDefs("query
name").Delete. But what about defining and or accessing the query once it is
created. Here is the sample code:

DoCmd.Close A_FORM, FormName

If db.QueryDefs.Count > 0 Then
For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "AddInv") Then
delquery = 1
End If
Next I
End If

If (delquery = 1) Then
db.DeleteQueryDef "AddInv"
End If

Set InvQuery = db.CreateQueryDef()
InvQuery.Name = "AddInv"
Querystr = "SELECT * FROM [Invoices]"
Querystr = Querystr & " WHERE (([InvoiceNo] = " & invnum & ") and ([Client
Code] = '" & clcode
Querystr = Querystr & "') AND ([Year] = '" & EditYearVal & "'));"
InvQuery.SQL = Querystr
db.QueryDefs.Append InvQuery
' MsgBox QueryStr, 48, ""


DoCmd.OpenForm "Invoice Form", A_NORMAL, , , A_EDIT
Forms("Invoice Form").MenuBar = "Empty_Menu"

End Function

As I may have mentioned earlier in this conversation, I saw the use of the
following code as shown above within a loop or other call:

For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "AddInv") Then
delquery = 1
End If

If I had to delete the query structure, because no results were returned
such as
if I = 0, I have seen references to the delete query in one of two ways:

DeleteQuerryDef(AddInv(I))

or sometimes a simple reference such as the following:

DeleteQueryDef(AddInv)

in either case, the (I) reference does not work with the Access 2000 code:

CurrentDb.QueryDefs("addInv(I)").delete

I was assuming that a particular occurance of a query with the (I) reference
was to be deleted as shown above, but in the code I have given you as an
example has the (I) reference in the the loop as again illustrated below, but
the delete query is with no (I) value. Why is this? :

(the loop here)

For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "AddInv") Then
delquery = 1
End If

(the deltete query request below)

If (delquery = 1) Then
db.DeleteQueryDef "AddInv"
End If

If you can give me a good explanation of the code above with the query defs
usage and if any changes (besides how the query def is delete in access 2000)
should be made to the query def commands for access 2000, as well as what
they all mean in an access 97/2000 context, I would really appreciate it.

Edward Letendre.


Ken Snell said:
I would change this line
Set db = DBEngine.Workspaces(0).Databases(0)
to this
Set db = CurrentDb

Your original code used 95, 96, 97, 98, and 99 for the years 1995, 1996,
etc., but the code here will expect to see the full 1995 numbers, not the 95
that was in the original code. Not sure if that will meet your needs.

This code:
Do While Yearcnt <> 1994
If Yearval = Yearcnt Then
SubFormName = "ATS LINES " & Yearcnt 'this should add a value
for the sub form name

Else
Yearcnt = Yearcnt - 1
Loop

will never get out of the loop. You don't decrement the yearcnt value when
it matches the Yearval value, so the loop will be endless. It should be
something like this:
Do While Yearcnt <> 1994
If Yearval = Yearcnt Then
SubFormName = "ATS LINES " & Yearcnt 'this should add a value
for the sub form name
Exit Do
Else
Yearcnt = Yearcnt - 1
Loop


The QueryDefs loop is looking to see if your database contains a query named
AddInv. If it's found, the database deletes it from the database. This code
is a bit of a round-a-bout way to do that, but it'll work.

--

Ken Snell
<MS ACCESS MVP>




Edward Letendre said:
Thanks for the reply, I am adding some new stuff based on the info you
gave
to me last. Here is an example of an loop with an if statement that I
will
add to the code as a test to see if it will work, but could you please
comment on my code and make any corrections you see fit:

Dim db As Database
Dim FormName As String, SubFormName
Dim eol As Single, bol As Single
Dim statint As Single, kmcount As Single
Dim getval As Variant
Dim fm As String
Dim yearstr As String
Dim Yearval As Integer
Dim Yearcnt As Integer

Set db = DBEngine.Workspaces(0).Databases(0)


FormName = Screen.ActiveForm.Name


getval = Forms(FormName)![Year]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "No Year!", 48, ""
Exit Function
Else
yearstr = getval
End If

Yearval = Val(yearstr)

Yearcnt = 2005 'this code sets the year count variable to the year 2005

' The loop below should go from 2005 to 1994, year by year until a match
is
found
in the Yearval variable

Do While Yearcnt <> 1994
If Yearval = Yearcnt Then
SubFormName = "ATS LINES " & Yearcnt 'this should add a value for
the sub form name

Else
Yearcnt = Yearcnt - 1
Loop

Here is the original case statement, that loads the sub form name variable
based on the yearval variable that I am attempting to duplicate from the
code above:

Select Case Yearval
Case 95: SubFormName = "ATS LINES 1995"
Case 96: SubFormName = "ATS LINES 1996"
Case 97: SubFormName = "ATS LINES 1997"
Case 98: SubFormName = "ATS LINES 1998"
Case 99: SubFormName = "ATS LINES 1999"
Case 2000: SubFormName = "ATS LINES 2000"
Case 2001: SubFormName = "ATS LINES 2001"
Case 2002: SubFormName = "ATS LINES 2002"
Case 2003: SubFormName = "ATS LINES 2003"
Case 2004: SubFormName = "ATS LINES 2004"
Case 2005: SubFormName = "ATS LINES 2005"
End Select

The other thing is this is some code I came across in looking at this
project, can you please explain what it means:

If db.QueryDefs.Count > 0 Then
For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "AddInv") Then
delquery = 1
End If
Next I
End If

If (delquery = 1) Then
db.DeleteQueryDef "AddInv"
End If

My confusion is with the statement: If (db.QueryDefs(I).Name = "AddInv")
Then

Why was the db.QueryDefs(I).Name used in the above code and then
the delete query request was used as follows:

db.DeleteQueryDEf "AddInv" use, where in other code I have seen the
following used as well

db.DeleteQueryDef(I).Name"AddInv".

where as in access 2000, you have to delete the query using the followng
code:

CurrentDb.QueryDefs("AddInv").Delete

but you cannot delete a query this way:

CurrentDb.QueryDef(I).Name("AddInv").Delete

Thus why use the QueryDef(I).Name part of the function such as with access
97 but not in access 2000 or however it would be coded in access 2000 as
compared to access 97. I understand this is confusing but as I say how or
why do the programmers add the (I).Name part to the querydef and then
delete
the query def if no occurance is not fount with the (I).Name added to it?

Edward Letendre.
 
E

Edward Letendre

I am replying again, as I have a second question that poped up when I was
making some changes. This applied to another problem and I did not add it to
my earlier question as it just happend and I had sent the other message a few
days ago. So here goes.

I was compiling the code that was changed according to the changes you
suggested as well as the loop code that I wanted to add. These changes
appear to be okay, as the code works as created, but there are new problems
with a function that is used to handle errors. The function is lised in a
code example as follows:

Option Compare Database 'Use database order for string comparisons

Private Sub Add_Record_Click()
On Error GoTo Err_Add_Record_Click


DoCmd.GoToRecord , , A_NEWREC

Exit_Add_Record_Click:
Exit Sub

Err_Add_Record_Click:
MsgBox Error$
Resume Exit_Add_Record_Click

End Sub

The error occurs at teh MsgBox Error$ line. I was assuming that the Error$
was a function call to a possible Access 97 function. The error message is
basically that this Error$ function cannot be found and a window pops up
asking what libraries I should be adding to help locate the missing function.
If I solve this problem, I should be on my way to finishing this project,
but you never know.

Edward Letendre.


Ken Snell said:
I would change this line
Set db = DBEngine.Workspaces(0).Databases(0)
to this
Set db = CurrentDb

Your original code used 95, 96, 97, 98, and 99 for the years 1995, 1996,
etc., but the code here will expect to see the full 1995 numbers, not the 95
that was in the original code. Not sure if that will meet your needs.

This code:
Do While Yearcnt <> 1994
If Yearval = Yearcnt Then
SubFormName = "ATS LINES " & Yearcnt 'this should add a value
for the sub form name

Else
Yearcnt = Yearcnt - 1
Loop

will never get out of the loop. You don't decrement the yearcnt value when
it matches the Yearval value, so the loop will be endless. It should be
something like this:
Do While Yearcnt <> 1994
If Yearval = Yearcnt Then
SubFormName = "ATS LINES " & Yearcnt 'this should add a value
for the sub form name
Exit Do
Else
Yearcnt = Yearcnt - 1
Loop


The QueryDefs loop is looking to see if your database contains a query named
AddInv. If it's found, the database deletes it from the database. This code
is a bit of a round-a-bout way to do that, but it'll work.

--

Ken Snell
<MS ACCESS MVP>




Edward Letendre said:
Thanks for the reply, I am adding some new stuff based on the info you
gave
to me last. Here is an example of an loop with an if statement that I
will
add to the code as a test to see if it will work, but could you please
comment on my code and make any corrections you see fit:

Dim db As Database
Dim FormName As String, SubFormName
Dim eol As Single, bol As Single
Dim statint As Single, kmcount As Single
Dim getval As Variant
Dim fm As String
Dim yearstr As String
Dim Yearval As Integer
Dim Yearcnt As Integer

Set db = DBEngine.Workspaces(0).Databases(0)


FormName = Screen.ActiveForm.Name


getval = Forms(FormName)![Year]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "No Year!", 48, ""
Exit Function
Else
yearstr = getval
End If

Yearval = Val(yearstr)

Yearcnt = 2005 'this code sets the year count variable to the year 2005

' The loop below should go from 2005 to 1994, year by year until a match
is
found
in the Yearval variable

Do While Yearcnt <> 1994
If Yearval = Yearcnt Then
SubFormName = "ATS LINES " & Yearcnt 'this should add a value for
the sub form name

Else
Yearcnt = Yearcnt - 1
Loop

Here is the original case statement, that loads the sub form name variable
based on the yearval variable that I am attempting to duplicate from the
code above:

Select Case Yearval
Case 95: SubFormName = "ATS LINES 1995"
Case 96: SubFormName = "ATS LINES 1996"
Case 97: SubFormName = "ATS LINES 1997"
Case 98: SubFormName = "ATS LINES 1998"
Case 99: SubFormName = "ATS LINES 1999"
Case 2000: SubFormName = "ATS LINES 2000"
Case 2001: SubFormName = "ATS LINES 2001"
Case 2002: SubFormName = "ATS LINES 2002"
Case 2003: SubFormName = "ATS LINES 2003"
Case 2004: SubFormName = "ATS LINES 2004"
Case 2005: SubFormName = "ATS LINES 2005"
End Select

The other thing is this is some code I came across in looking at this
project, can you please explain what it means:

If db.QueryDefs.Count > 0 Then
For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "AddInv") Then
delquery = 1
End If
Next I
End If

If (delquery = 1) Then
db.DeleteQueryDef "AddInv"
End If

My confusion is with the statement: If (db.QueryDefs(I).Name = "AddInv")
Then

Why was the db.QueryDefs(I).Name used in the above code and then
the delete query request was used as follows:

db.DeleteQueryDEf "AddInv" use, where in other code I have seen the
following used as well

db.DeleteQueryDef(I).Name"AddInv".

where as in access 2000, you have to delete the query using the followng
code:

CurrentDb.QueryDefs("AddInv").Delete

but you cannot delete a query this way:

CurrentDb.QueryDef(I).Name("AddInv").Delete

Thus why use the QueryDef(I).Name part of the function such as with access
97 but not in access 2000 or however it would be coded in access 2000 as
compared to access 97. I understand this is confusing but as I say how or
why do the programmers add the (I).Name part to the querydef and then
delete
the query def if no occurance is not fount with the (I).Name added to it?

Edward Letendre.
 
D

Douglas J. Steele

Even in Access 97, Error$ wasn't recommended. Try Err.Description instead.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)



Edward Letendre said:
I am replying again, as I have a second question that poped up when I was
making some changes. This applied to another problem and I did not add it
to
my earlier question as it just happend and I had sent the other message a
few
days ago. So here goes.

I was compiling the code that was changed according to the changes you
suggested as well as the loop code that I wanted to add. These changes
appear to be okay, as the code works as created, but there are new
problems
with a function that is used to handle errors. The function is lised in a
code example as follows:

Option Compare Database 'Use database order for string comparisons

Private Sub Add_Record_Click()
On Error GoTo Err_Add_Record_Click


DoCmd.GoToRecord , , A_NEWREC

Exit_Add_Record_Click:
Exit Sub

Err_Add_Record_Click:
MsgBox Error$
Resume Exit_Add_Record_Click

End Sub

The error occurs at teh MsgBox Error$ line. I was assuming that the
Error$
was a function call to a possible Access 97 function. The error message
is
basically that this Error$ function cannot be found and a window pops up
asking what libraries I should be adding to help locate the missing
function.
If I solve this problem, I should be on my way to finishing this project,
but you never know.

Edward Letendre.


Ken Snell said:
I would change this line
Set db = DBEngine.Workspaces(0).Databases(0)
to this
Set db = CurrentDb

Your original code used 95, 96, 97, 98, and 99 for the years 1995, 1996,
etc., but the code here will expect to see the full 1995 numbers, not the
95
that was in the original code. Not sure if that will meet your needs.

This code:
Do While Yearcnt <> 1994
If Yearval = Yearcnt Then
SubFormName = "ATS LINES " & Yearcnt 'this should add a
value
for the sub form name

Else
Yearcnt = Yearcnt - 1
Loop

will never get out of the loop. You don't decrement the yearcnt value
when
it matches the Yearval value, so the loop will be endless. It should be
something like this:
Do While Yearcnt <> 1994
If Yearval = Yearcnt Then
SubFormName = "ATS LINES " & Yearcnt 'this should add a
value
for the sub form name
Exit Do
Else
Yearcnt = Yearcnt - 1
Loop


The QueryDefs loop is looking to see if your database contains a query
named
AddInv. If it's found, the database deletes it from the database. This
code
is a bit of a round-a-bout way to do that, but it'll work.

--

Ken Snell
<MS ACCESS MVP>




Edward Letendre said:
Thanks for the reply, I am adding some new stuff based on the info you
gave
to me last. Here is an example of an loop with an if statement that I
will
add to the code as a test to see if it will work, but could you please
comment on my code and make any corrections you see fit:

Dim db As Database
Dim FormName As String, SubFormName
Dim eol As Single, bol As Single
Dim statint As Single, kmcount As Single
Dim getval As Variant
Dim fm As String
Dim yearstr As String
Dim Yearval As Integer
Dim Yearcnt As Integer

Set db = DBEngine.Workspaces(0).Databases(0)


FormName = Screen.ActiveForm.Name


getval = Forms(FormName)![Year]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "No Year!", 48, ""
Exit Function
Else
yearstr = getval
End If

Yearval = Val(yearstr)

Yearcnt = 2005 'this code sets the year count variable to the year 2005

' The loop below should go from 2005 to 1994, year by year until a
match
is
found
in the Yearval variable

Do While Yearcnt <> 1994
If Yearval = Yearcnt Then
SubFormName = "ATS LINES " & Yearcnt 'this should add a value
for
the sub form name

Else
Yearcnt = Yearcnt - 1
Loop

Here is the original case statement, that loads the sub form name
variable
based on the yearval variable that I am attempting to duplicate from
the
code above:

Select Case Yearval
Case 95: SubFormName = "ATS LINES 1995"
Case 96: SubFormName = "ATS LINES 1996"
Case 97: SubFormName = "ATS LINES 1997"
Case 98: SubFormName = "ATS LINES 1998"
Case 99: SubFormName = "ATS LINES 1999"
Case 2000: SubFormName = "ATS LINES 2000"
Case 2001: SubFormName = "ATS LINES 2001"
Case 2002: SubFormName = "ATS LINES 2002"
Case 2003: SubFormName = "ATS LINES 2003"
Case 2004: SubFormName = "ATS LINES 2004"
Case 2005: SubFormName = "ATS LINES 2005"
End Select

The other thing is this is some code I came across in looking at this
project, can you please explain what it means:

If db.QueryDefs.Count > 0 Then
For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "AddInv") Then
delquery = 1
End If
Next I
End If

If (delquery = 1) Then
db.DeleteQueryDef "AddInv"
End If

My confusion is with the statement: If (db.QueryDefs(I).Name =
"AddInv")
Then

Why was the db.QueryDefs(I).Name used in the above code and then
the delete query request was used as follows:

db.DeleteQueryDEf "AddInv" use, where in other code I have seen the
following used as well

db.DeleteQueryDef(I).Name"AddInv".

where as in access 2000, you have to delete the query using the
followng
code:

CurrentDb.QueryDefs("AddInv").Delete

but you cannot delete a query this way:

CurrentDb.QueryDef(I).Name("AddInv").Delete

Thus why use the QueryDef(I).Name part of the function such as with
access
97 but not in access 2000 or however it would be coded in access 2000
as
compared to access 97. I understand this is confusing but as I say how
or
why do the programmers add the (I).Name part to the querydef and then
delete
the query def if no occurance is not fount with the (I).Name added to
it?

Edward Letendre.
 
K

Ken Snell [MVP]

I have been tied up on some things and haven't had chance to prepare a
reply, but I will....

--

Ken Snell
<MS ACCESS MVP>

Edward Letendre said:
Thanks for the information up to this point. I am really learning a lot
and
access 2000 code is looking much like Oracle stuff with functions, code,
etc.
in a database standard way. Anyway, I asked this earlier, but here is
full
example of some code that was used in this application. I see that a
variable related to the querydef type of variable was used in this
function
or sub-routine or whatever you want to call it,a s it is in the module
section of Access 97/2000. The use of the variable is also through this
particular section of code and I believe that a querydef is used to access
a
query of a table or a table structure (rows, columns, etc.). So in going
from Access 97 to Access 2000, is this code sound (I know that the way to
delete a query, as you mentioned before is to use
CurrentDb.QueryDefs("query
name").Delete. But what about defining and or accessing the query once it
is
created. Here is the sample code:

DoCmd.Close A_FORM, FormName

If db.QueryDefs.Count > 0 Then
For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "AddInv") Then
delquery = 1
End If
Next I
End If

If (delquery = 1) Then
db.DeleteQueryDef "AddInv"
End If

Set InvQuery = db.CreateQueryDef()
InvQuery.Name = "AddInv"
Querystr = "SELECT * FROM [Invoices]"
Querystr = Querystr & " WHERE (([InvoiceNo] = " & invnum & ") and ([Client
Code] = '" & clcode
Querystr = Querystr & "') AND ([Year] = '" & EditYearVal & "'));"
InvQuery.SQL = Querystr
db.QueryDefs.Append InvQuery
' MsgBox QueryStr, 48, ""


DoCmd.OpenForm "Invoice Form", A_NORMAL, , , A_EDIT
Forms("Invoice Form").MenuBar = "Empty_Menu"

End Function

As I may have mentioned earlier in this conversation, I saw the use of the
following code as shown above within a loop or other call:

For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "AddInv") Then
delquery = 1
End If

If I had to delete the query structure, because no results were returned
such as
if I = 0, I have seen references to the delete query in one of two ways:

DeleteQuerryDef(AddInv(I))

or sometimes a simple reference such as the following:

DeleteQueryDef(AddInv)

in either case, the (I) reference does not work with the Access 2000 code:

CurrentDb.QueryDefs("addInv(I)").delete

I was assuming that a particular occurance of a query with the (I)
reference
was to be deleted as shown above, but in the code I have given you as an
example has the (I) reference in the the loop as again illustrated below,
but
the delete query is with no (I) value. Why is this? :

(the loop here)

For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "AddInv") Then
delquery = 1
End If

(the deltete query request below)

If (delquery = 1) Then
db.DeleteQueryDef "AddInv"
End If

If you can give me a good explanation of the code above with the query
defs
usage and if any changes (besides how the query def is delete in access
2000)
should be made to the query def commands for access 2000, as well as what
they all mean in an access 97/2000 context, I would really appreciate it.

Edward Letendre.


Ken Snell said:
I would change this line
Set db = DBEngine.Workspaces(0).Databases(0)
to this
Set db = CurrentDb

Your original code used 95, 96, 97, 98, and 99 for the years 1995, 1996,
etc., but the code here will expect to see the full 1995 numbers, not the
95
that was in the original code. Not sure if that will meet your needs.

This code:
Do While Yearcnt <> 1994
If Yearval = Yearcnt Then
SubFormName = "ATS LINES " & Yearcnt 'this should add a
value
for the sub form name

Else
Yearcnt = Yearcnt - 1
Loop

will never get out of the loop. You don't decrement the yearcnt value
when
it matches the Yearval value, so the loop will be endless. It should be
something like this:
Do While Yearcnt <> 1994
If Yearval = Yearcnt Then
SubFormName = "ATS LINES " & Yearcnt 'this should add a
value
for the sub form name
Exit Do
Else
Yearcnt = Yearcnt - 1
Loop


The QueryDefs loop is looking to see if your database contains a query
named
AddInv. If it's found, the database deletes it from the database. This
code
is a bit of a round-a-bout way to do that, but it'll work.

--

Ken Snell
<MS ACCESS MVP>




Edward Letendre said:
Thanks for the reply, I am adding some new stuff based on the info you
gave
to me last. Here is an example of an loop with an if statement that I
will
add to the code as a test to see if it will work, but could you please
comment on my code and make any corrections you see fit:

Dim db As Database
Dim FormName As String, SubFormName
Dim eol As Single, bol As Single
Dim statint As Single, kmcount As Single
Dim getval As Variant
Dim fm As String
Dim yearstr As String
Dim Yearval As Integer
Dim Yearcnt As Integer

Set db = DBEngine.Workspaces(0).Databases(0)


FormName = Screen.ActiveForm.Name


getval = Forms(FormName)![Year]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "No Year!", 48, ""
Exit Function
Else
yearstr = getval
End If

Yearval = Val(yearstr)

Yearcnt = 2005 'this code sets the year count variable to the year 2005

' The loop below should go from 2005 to 1994, year by year until a
match
is
found
in the Yearval variable

Do While Yearcnt <> 1994
If Yearval = Yearcnt Then
SubFormName = "ATS LINES " & Yearcnt 'this should add a value
for
the sub form name

Else
Yearcnt = Yearcnt - 1
Loop

Here is the original case statement, that loads the sub form name
variable
based on the yearval variable that I am attempting to duplicate from
the
code above:

Select Case Yearval
Case 95: SubFormName = "ATS LINES 1995"
Case 96: SubFormName = "ATS LINES 1996"
Case 97: SubFormName = "ATS LINES 1997"
Case 98: SubFormName = "ATS LINES 1998"
Case 99: SubFormName = "ATS LINES 1999"
Case 2000: SubFormName = "ATS LINES 2000"
Case 2001: SubFormName = "ATS LINES 2001"
Case 2002: SubFormName = "ATS LINES 2002"
Case 2003: SubFormName = "ATS LINES 2003"
Case 2004: SubFormName = "ATS LINES 2004"
Case 2005: SubFormName = "ATS LINES 2005"
End Select

The other thing is this is some code I came across in looking at this
project, can you please explain what it means:

If db.QueryDefs.Count > 0 Then
For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "AddInv") Then
delquery = 1
End If
Next I
End If

If (delquery = 1) Then
db.DeleteQueryDef "AddInv"
End If

My confusion is with the statement: If (db.QueryDefs(I).Name =
"AddInv")
Then

Why was the db.QueryDefs(I).Name used in the above code and then
the delete query request was used as follows:

db.DeleteQueryDEf "AddInv" use, where in other code I have seen the
following used as well

db.DeleteQueryDef(I).Name"AddInv".

where as in access 2000, you have to delete the query using the
followng
code:

CurrentDb.QueryDefs("AddInv").Delete

but you cannot delete a query this way:

CurrentDb.QueryDef(I).Name("AddInv").Delete

Thus why use the QueryDef(I).Name part of the function such as with
access
97 but not in access 2000 or however it would be coded in access 2000
as
compared to access 97. I understand this is confusing but as I say how
or
why do the programmers add the (I).Name part to the querydef and then
delete
the query def if no occurance is not fount with the (I).Name added to
it?

Edward Letendre.
 
K

Ken Snell [MVP]

You have a large variety of "query def" stuff here, so I am not sure I can
really answer your questions adequately unless we focus on just one or two
specific examples.

Your reference to "DeleteQuerryDef(AddInv(I))" and "DeleteQueryDef(AddInv)"
doesn't work in ACCESS unless your code contains a subroutine named
"DeleteQuerryDef" or "DeleteQueryDef". ACCESS does not have a built-in
subroutine by either of these names.

With regard to your other questions, if you can focus on one or two pieces
of the code, I will try to give an answer to those.
--

Ken Snell
<MS ACCESS MVP>



Edward Letendre said:
Thanks for the information up to this point. I am really learning a lot
and
access 2000 code is looking much like Oracle stuff with functions, code,
etc.
in a database standard way. Anyway, I asked this earlier, but here is
full
example of some code that was used in this application. I see that a
variable related to the querydef type of variable was used in this
function
or sub-routine or whatever you want to call it,a s it is in the module
section of Access 97/2000. The use of the variable is also through this
particular section of code and I believe that a querydef is used to access
a
query of a table or a table structure (rows, columns, etc.). So in going
from Access 97 to Access 2000, is this code sound (I know that the way to
delete a query, as you mentioned before is to use
CurrentDb.QueryDefs("query
name").Delete. But what about defining and or accessing the query once it
is
created. Here is the sample code:

DoCmd.Close A_FORM, FormName

If db.QueryDefs.Count > 0 Then
For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "AddInv") Then
delquery = 1
End If
Next I
End If

If (delquery = 1) Then
db.DeleteQueryDef "AddInv"
End If

Set InvQuery = db.CreateQueryDef()
InvQuery.Name = "AddInv"
Querystr = "SELECT * FROM [Invoices]"
Querystr = Querystr & " WHERE (([InvoiceNo] = " & invnum & ") and ([Client
Code] = '" & clcode
Querystr = Querystr & "') AND ([Year] = '" & EditYearVal & "'));"
InvQuery.SQL = Querystr
db.QueryDefs.Append InvQuery
' MsgBox QueryStr, 48, ""


DoCmd.OpenForm "Invoice Form", A_NORMAL, , , A_EDIT
Forms("Invoice Form").MenuBar = "Empty_Menu"

End Function

As I may have mentioned earlier in this conversation, I saw the use of the
following code as shown above within a loop or other call:

For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "AddInv") Then
delquery = 1
End If

If I had to delete the query structure, because no results were returned
such as
if I = 0, I have seen references to the delete query in one of two ways:

DeleteQuerryDef(AddInv(I))

or sometimes a simple reference such as the following:

DeleteQueryDef(AddInv)

in either case, the (I) reference does not work with the Access 2000 code:

CurrentDb.QueryDefs("addInv(I)").delete

I was assuming that a particular occurance of a query with the (I)
reference
was to be deleted as shown above, but in the code I have given you as an
example has the (I) reference in the the loop as again illustrated below,
but
the delete query is with no (I) value. Why is this? :

(the loop here)

For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "AddInv") Then
delquery = 1
End If

(the deltete query request below)

If (delquery = 1) Then
db.DeleteQueryDef "AddInv"
End If

If you can give me a good explanation of the code above with the query
defs
usage and if any changes (besides how the query def is delete in access
2000)
should be made to the query def commands for access 2000, as well as what
they all mean in an access 97/2000 context, I would really appreciate it.

Edward Letendre.


Ken Snell said:
I would change this line
Set db = DBEngine.Workspaces(0).Databases(0)
to this
Set db = CurrentDb

Your original code used 95, 96, 97, 98, and 99 for the years 1995, 1996,
etc., but the code here will expect to see the full 1995 numbers, not the
95
that was in the original code. Not sure if that will meet your needs.

This code:
Do While Yearcnt <> 1994
If Yearval = Yearcnt Then
SubFormName = "ATS LINES " & Yearcnt 'this should add a
value
for the sub form name

Else
Yearcnt = Yearcnt - 1
Loop

will never get out of the loop. You don't decrement the yearcnt value
when
it matches the Yearval value, so the loop will be endless. It should be
something like this:
Do While Yearcnt <> 1994
If Yearval = Yearcnt Then
SubFormName = "ATS LINES " & Yearcnt 'this should add a
value
for the sub form name
Exit Do
Else
Yearcnt = Yearcnt - 1
Loop


The QueryDefs loop is looking to see if your database contains a query
named
AddInv. If it's found, the database deletes it from the database. This
code
is a bit of a round-a-bout way to do that, but it'll work.

--

Ken Snell
<MS ACCESS MVP>




Edward Letendre said:
Thanks for the reply, I am adding some new stuff based on the info you
gave
to me last. Here is an example of an loop with an if statement that I
will
add to the code as a test to see if it will work, but could you please
comment on my code and make any corrections you see fit:

Dim db As Database
Dim FormName As String, SubFormName
Dim eol As Single, bol As Single
Dim statint As Single, kmcount As Single
Dim getval As Variant
Dim fm As String
Dim yearstr As String
Dim Yearval As Integer
Dim Yearcnt As Integer

Set db = DBEngine.Workspaces(0).Databases(0)


FormName = Screen.ActiveForm.Name


getval = Forms(FormName)![Year]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "No Year!", 48, ""
Exit Function
Else
yearstr = getval
End If

Yearval = Val(yearstr)

Yearcnt = 2005 'this code sets the year count variable to the year 2005

' The loop below should go from 2005 to 1994, year by year until a
match
is
found
in the Yearval variable

Do While Yearcnt <> 1994
If Yearval = Yearcnt Then
SubFormName = "ATS LINES " & Yearcnt 'this should add a value
for
the sub form name

Else
Yearcnt = Yearcnt - 1
Loop

Here is the original case statement, that loads the sub form name
variable
based on the yearval variable that I am attempting to duplicate from
the
code above:

Select Case Yearval
Case 95: SubFormName = "ATS LINES 1995"
Case 96: SubFormName = "ATS LINES 1996"
Case 97: SubFormName = "ATS LINES 1997"
Case 98: SubFormName = "ATS LINES 1998"
Case 99: SubFormName = "ATS LINES 1999"
Case 2000: SubFormName = "ATS LINES 2000"
Case 2001: SubFormName = "ATS LINES 2001"
Case 2002: SubFormName = "ATS LINES 2002"
Case 2003: SubFormName = "ATS LINES 2003"
Case 2004: SubFormName = "ATS LINES 2004"
Case 2005: SubFormName = "ATS LINES 2005"
End Select

The other thing is this is some code I came across in looking at this
project, can you please explain what it means:

If db.QueryDefs.Count > 0 Then
For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "AddInv") Then
delquery = 1
End If
Next I
End If

If (delquery = 1) Then
db.DeleteQueryDef "AddInv"
End If

My confusion is with the statement: If (db.QueryDefs(I).Name =
"AddInv")
Then

Why was the db.QueryDefs(I).Name used in the above code and then
the delete query request was used as follows:

db.DeleteQueryDEf "AddInv" use, where in other code I have seen the
following used as well

db.DeleteQueryDef(I).Name"AddInv".

where as in access 2000, you have to delete the query using the
followng
code:

CurrentDb.QueryDefs("AddInv").Delete

but you cannot delete a query this way:

CurrentDb.QueryDef(I).Name("AddInv").Delete

Thus why use the QueryDef(I).Name part of the function such as with
access
97 but not in access 2000 or however it would be coded in access 2000
as
compared to access 97. I understand this is confusing but as I say how
or
why do the programmers add the (I).Name part to the querydef and then
delete
the query def if no occurance is not fount with the (I).Name added to
it?

Edward Letendre.
 
E

Edward Letendre

Okay, here is a full example of the code that was used in a form:

Private Sub print_Click()
On Error GoTo Err_print_Click

Dim db As Database
Dim getval As Variant
Dim delquery As Integer
Dim clcode As String
Dim yearstr As String
Dim jobnum As Integer
Dim Querystr As String
Dim StatusQuery As QueryDef
Dim jobtabname As String
Dim FormName As String
Dim I As Integer

Dim DocName As String
DocName = "Data Output Report"

Set db = CurrentDb
Set StatusQuery = db.CreateQueryDef()
StatusQuery.Name = "Data Output Query"

DoCmd.DoMenuItem A_FORMBAR, A_FILE, A_SAVERECORD, , A_MENU_VER20

delquery = 0

If db.QueryDefs.Count > 0 Then
For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "Data Output Query") Then
delquery = 1
End If
Next I
End If

note: The command DeleteQuerfyDef("Data Output Query") is shown as it was
originally coded in Access 97. I am very confused on this command. From
what you had said before, the command should be used to remove or clean up a
query if the record that is being searched for is either found or not found,
depending on the code. So could you please check out this entire line of
code to give me a basic understanding of what it might do in this example.

If (delquery = 1) Then
DeleteQueryDef("Data Output Query")
End If

FormName = Screen.ActiveForm.Name
getval = Forms(FormName)![Client Code]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "You MUST supply a Client Code!", 48, ""
Exit Sub
Else
clcode = getval
End If

getval = Forms(FormName)![Job Code]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "You MUST supply a Job Number!", 48, ""
Exit Sub
Else
jobnum = getval
End If

getval = Forms(FormName)![Year]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "No Year!", 48, ""
Exit Sub
Else
yearstr = getval
End If

jobtabname = "ATS JOBS " & yearstr

Querystr = "SELECT dtoutput.*, Prospect,[Client Project Number], [Data Type],"
Querystr = Querystr & "[Data Source],[Data Rights], [Job Type],[Proc Co
Name],"
Querystr = Querystr & "[Proc Co Attn], [Acct Code], [AFE], CLIENT.[Client
Name] "
Querystr = Querystr & " From ([Data Output] as dtoutput "
Querystr = Querystr & " INNER JOIN CLIENT ON dtoutput.[Client Code] =
CLIENT.[Client Code]) "
Querystr = Querystr & " INNER JOIN [" & jobtabname
Querystr = Querystr & "] AS jobs ON (jobs.[Client Code] = dtoutput.[Client
Code])"
Querystr = Querystr & " AND (jobs.[Job Code] = dtoutput.[Job Code])"
Querystr = Querystr & " WHERE ((jobs.[Client Code] = '" & clcode
Querystr = Querystr & "') AND (jobs.[Job Code] = " & jobnum & "));"


StatusQuery.SQL = Querystr
db.QueryDefs.Append StatusQuery
StatusQuery.Close

DoCmd.OpenReport DocName, A_PREVIEW

Exit_print_Click:
Exit Sub

Err_print_Click:
MsgBox Error$
Resume Exit_print_Click

End Sub

Ken Snell said:
You have a large variety of "query def" stuff here, so I am not sure I can
really answer your questions adequately unless we focus on just one or two
specific examples.

Your reference to "DeleteQuerryDef(AddInv(I))" and "DeleteQueryDef(AddInv)"
doesn't work in ACCESS unless your code contains a subroutine named
"DeleteQuerryDef" or "DeleteQueryDef". ACCESS does not have a built-in
subroutine by either of these names.

With regard to your other questions, if you can focus on one or two pieces
of the code, I will try to give an answer to those.
--

Ken Snell
<MS ACCESS MVP>



Edward Letendre said:
Thanks for the information up to this point. I am really learning a lot
and
access 2000 code is looking much like Oracle stuff with functions, code,
etc.
in a database standard way. Anyway, I asked this earlier, but here is
full
example of some code that was used in this application. I see that a
variable related to the querydef type of variable was used in this
function
or sub-routine or whatever you want to call it,a s it is in the module
section of Access 97/2000. The use of the variable is also through this
particular section of code and I believe that a querydef is used to access
a
query of a table or a table structure (rows, columns, etc.). So in going
from Access 97 to Access 2000, is this code sound (I know that the way to
delete a query, as you mentioned before is to use
CurrentDb.QueryDefs("query
name").Delete. But what about defining and or accessing the query once it
is
created. Here is the sample code:

DoCmd.Close A_FORM, FormName

If db.QueryDefs.Count > 0 Then
For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "AddInv") Then
delquery = 1
End If
Next I
End If

If (delquery = 1) Then
db.DeleteQueryDef "AddInv"
End If

Set InvQuery = db.CreateQueryDef()
InvQuery.Name = "AddInv"
Querystr = "SELECT * FROM [Invoices]"
Querystr = Querystr & " WHERE (([InvoiceNo] = " & invnum & ") and ([Client
Code] = '" & clcode
Querystr = Querystr & "') AND ([Year] = '" & EditYearVal & "'));"
InvQuery.SQL = Querystr
db.QueryDefs.Append InvQuery
' MsgBox QueryStr, 48, ""


DoCmd.OpenForm "Invoice Form", A_NORMAL, , , A_EDIT
Forms("Invoice Form").MenuBar = "Empty_Menu"

End Function

As I may have mentioned earlier in this conversation, I saw the use of the
following code as shown above within a loop or other call:

For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "AddInv") Then
delquery = 1
End If

If I had to delete the query structure, because no results were returned
such as
if I = 0, I have seen references to the delete query in one of two ways:

DeleteQuerryDef(AddInv(I))

or sometimes a simple reference such as the following:

DeleteQueryDef(AddInv)

in either case, the (I) reference does not work with the Access 2000 code:

CurrentDb.QueryDefs("addInv(I)").delete

I was assuming that a particular occurance of a query with the (I)
reference
was to be deleted as shown above, but in the code I have given you as an
example has the (I) reference in the the loop as again illustrated below,
but
the delete query is with no (I) value. Why is this? :

(the loop here)

For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "AddInv") Then
delquery = 1
End If

(the deltete query request below)

If (delquery = 1) Then
db.DeleteQueryDef "AddInv"
End If

If you can give me a good explanation of the code above with the query
defs
usage and if any changes (besides how the query def is delete in access
2000)
should be made to the query def commands for access 2000, as well as what
they all mean in an access 97/2000 context, I would really appreciate it.

Edward Letendre.


Ken Snell said:
I would change this line
Set db = DBEngine.Workspaces(0).Databases(0)
to this
Set db = CurrentDb

Your original code used 95, 96, 97, 98, and 99 for the years 1995, 1996,
etc., but the code here will expect to see the full 1995 numbers, not the
95
that was in the original code. Not sure if that will meet your needs.

This code:
Do While Yearcnt <> 1994
If Yearval = Yearcnt Then
SubFormName = "ATS LINES " & Yearcnt 'this should add a
value
for the sub form name

Else
Yearcnt = Yearcnt - 1
Loop

will never get out of the loop. You don't decrement the yearcnt value
when
it matches the Yearval value, so the loop will be endless. It should be
something like this:
Do While Yearcnt <> 1994
If Yearval = Yearcnt Then
SubFormName = "ATS LINES " & Yearcnt 'this should add a
value
for the sub form name
Exit Do
Else
Yearcnt = Yearcnt - 1
Loop


The QueryDefs loop is looking to see if your database contains a query
named
AddInv. If it's found, the database deletes it from the database. This
code
is a bit of a round-a-bout way to do that, but it'll work.

--

Ken Snell
<MS ACCESS MVP>




message Thanks for the reply, I am adding some new stuff based on the info you
gave
to me last. Here is an example of an loop with an if statement that I
will
add to the code as a test to see if it will work, but could you please
comment on my code and make any corrections you see fit:

Dim db As Database
Dim FormName As String, SubFormName
Dim eol As Single, bol As Single
Dim statint As Single, kmcount As Single
Dim getval As Variant
Dim fm As String
Dim yearstr As String
Dim Yearval As Integer
Dim Yearcnt As Integer

Set db = DBEngine.Workspaces(0).Databases(0)


FormName = Screen.ActiveForm.Name


getval = Forms(FormName)![Year]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "No Year!", 48, ""
Exit Function
Else
yearstr = getval
End If

Yearval = Val(yearstr)

Yearcnt = 2005 'this code sets the year count variable to the year 2005

' The loop below should go from 2005 to 1994, year by year until a
match
is
found
in the Yearval variable

Do While Yearcnt <> 1994
If Yearval = Yearcnt Then
SubFormName = "ATS LINES " & Yearcnt 'this should add a value
for
the sub form name

Else
Yearcnt = Yearcnt - 1
Loop

Here is the original case statement, that loads the sub form name
variable
based on the yearval variable that I am attempting to duplicate from
the
code above:

Select Case Yearval
Case 95: SubFormName = "ATS LINES 1995"
Case 96: SubFormName = "ATS LINES 1996"
Case 97: SubFormName = "ATS LINES 1997"
Case 98: SubFormName = "ATS LINES 1998"
Case 99: SubFormName = "ATS LINES 1999"
Case 2000: SubFormName = "ATS LINES 2000"
Case 2001: SubFormName = "ATS LINES 2001"
Case 2002: SubFormName = "ATS LINES 2002"
Case 2003: SubFormName = "ATS LINES 2003"
Case 2004: SubFormName = "ATS LINES 2004"
Case 2005: SubFormName = "ATS LINES 2005"
End Select

The other thing is this is some code I came across in looking at this
project, can you please explain what it means:

If db.QueryDefs.Count > 0 Then
For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "AddInv") Then
delquery = 1
End If
Next I
End If

If (delquery = 1) Then
db.DeleteQueryDef "AddInv"
End If

My confusion is with the statement: If (db.QueryDefs(I).Name =
"AddInv")
Then

Why was the db.QueryDefs(I).Name used in the above code and then
the delete query request was used as follows:

db.DeleteQueryDEf "AddInv" use, where in other code I have seen the
following used as well

db.DeleteQueryDef(I).Name"AddInv".

where as in access 2000, you have to delete the query using the
followng
code:

CurrentDb.QueryDefs("AddInv").Delete

but you cannot delete a query this way:

CurrentDb.QueryDef(I).Name("AddInv").Delete

Thus why use the QueryDef(I).Name part of the function such as with
access
97 but not in access 2000 or however it would be coded in access 2000
as
compared to access 97. I understand this is confusing but as I say how
or
why do the programmers add the (I).Name part to the querydef and then
delete
the query def if no occurance is not fount with the (I).Name added to
it?

Edward Letendre.
 
K

Ken Snell [MVP]

The following code block is what I would change:

If db.QueryDefs.Count > 0 Then
For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "Data Output Query") Then
delquery = 1
End If
Next I
End If

If (delquery = 1) Then
DeleteQueryDef("Data Output Query")
End If


I would change it to this:

If db.QueryDefs.Count > 0 Then
For I = db.QueryDefs.Count - 1 To 0 Step -1
If (db.QueryDefs(I).Name = "Data Output Query") Then
db.QueryDefs(I).Delete
Exit For
End If
Next I
End If

--

Ken Snell
<MS ACCESS MVP>




Edward Letendre said:
Okay, here is a full example of the code that was used in a form:

Private Sub print_Click()
On Error GoTo Err_print_Click

Dim db As Database
Dim getval As Variant
Dim delquery As Integer
Dim clcode As String
Dim yearstr As String
Dim jobnum As Integer
Dim Querystr As String
Dim StatusQuery As QueryDef
Dim jobtabname As String
Dim FormName As String
Dim I As Integer

Dim DocName As String
DocName = "Data Output Report"

Set db = CurrentDb
Set StatusQuery = db.CreateQueryDef()
StatusQuery.Name = "Data Output Query"

DoCmd.DoMenuItem A_FORMBAR, A_FILE, A_SAVERECORD, , A_MENU_VER20

delquery = 0

If db.QueryDefs.Count > 0 Then
For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "Data Output Query") Then
delquery = 1
End If
Next I
End If

note: The command DeleteQuerfyDef("Data Output Query") is shown as it was
originally coded in Access 97. I am very confused on this command. From
what you had said before, the command should be used to remove or clean up
a
query if the record that is being searched for is either found or not
found,
depending on the code. So could you please check out this entire line of
code to give me a basic understanding of what it might do in this example.

If (delquery = 1) Then
DeleteQueryDef("Data Output Query")
End If

FormName = Screen.ActiveForm.Name
getval = Forms(FormName)![Client Code]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "You MUST supply a Client Code!", 48, ""
Exit Sub
Else
clcode = getval
End If

getval = Forms(FormName)![Job Code]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "You MUST supply a Job Number!", 48, ""
Exit Sub
Else
jobnum = getval
End If

getval = Forms(FormName)![Year]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "No Year!", 48, ""
Exit Sub
Else
yearstr = getval
End If

jobtabname = "ATS JOBS " & yearstr

Querystr = "SELECT dtoutput.*, Prospect,[Client Project Number], [Data
Type],"
Querystr = Querystr & "[Data Source],[Data Rights], [Job Type],[Proc Co
Name],"
Querystr = Querystr & "[Proc Co Attn], [Acct Code], [AFE], CLIENT.[Client
Name] "
Querystr = Querystr & " From ([Data Output] as dtoutput "
Querystr = Querystr & " INNER JOIN CLIENT ON dtoutput.[Client Code] =
CLIENT.[Client Code]) "
Querystr = Querystr & " INNER JOIN [" & jobtabname
Querystr = Querystr & "] AS jobs ON (jobs.[Client Code] = dtoutput.[Client
Code])"
Querystr = Querystr & " AND (jobs.[Job Code] = dtoutput.[Job Code])"
Querystr = Querystr & " WHERE ((jobs.[Client Code] = '" & clcode
Querystr = Querystr & "') AND (jobs.[Job Code] = " & jobnum & "));"


StatusQuery.SQL = Querystr
db.QueryDefs.Append StatusQuery
StatusQuery.Close

DoCmd.OpenReport DocName, A_PREVIEW

Exit_print_Click:
Exit Sub

Err_print_Click:
MsgBox Error$
Resume Exit_print_Click

End Sub
 
E

Edward Letendre

Again, I want to thank you for your help up to this point. But I have
another problem. When I add the following code my access application (to be
percise to the code behind a form), as show below:

db.querydefs("Data Output Query").Delete

as shown in the code you saw before in this post, I get the following error
message about the .Delete part of the command above:

compile error: method or data member (Delete or .Delete) not found.

The weird thing is that this error does not occur in the other 20 to 30 or
so places that I have used this code to make the changes in the past to the
new delete method for the querydefs. Why would this delete method cause an
error at this point and not in other parts of the application with the same
code? This is very weird in deed.

Edward Letendre.


Ken Snell said:
The following code block is what I would change:

If db.QueryDefs.Count > 0 Then
For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "Data Output Query") Then
delquery = 1
End If
Next I
End If

If (delquery = 1) Then
DeleteQueryDef("Data Output Query")
End If


I would change it to this:

If db.QueryDefs.Count > 0 Then
For I = db.QueryDefs.Count - 1 To 0 Step -1
If (db.QueryDefs(I).Name = "Data Output Query") Then
db.QueryDefs(I).Delete
Exit For
End If
Next I
End If

--

Ken Snell
<MS ACCESS MVP>




Edward Letendre said:
Okay, here is a full example of the code that was used in a form:

Private Sub print_Click()
On Error GoTo Err_print_Click

Dim db As Database
Dim getval As Variant
Dim delquery As Integer
Dim clcode As String
Dim yearstr As String
Dim jobnum As Integer
Dim Querystr As String
Dim StatusQuery As QueryDef
Dim jobtabname As String
Dim FormName As String
Dim I As Integer

Dim DocName As String
DocName = "Data Output Report"

Set db = CurrentDb
Set StatusQuery = db.CreateQueryDef()
StatusQuery.Name = "Data Output Query"

DoCmd.DoMenuItem A_FORMBAR, A_FILE, A_SAVERECORD, , A_MENU_VER20

delquery = 0

If db.QueryDefs.Count > 0 Then
For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "Data Output Query") Then
delquery = 1
End If
Next I
End If

note: The command DeleteQuerfyDef("Data Output Query") is shown as it was
originally coded in Access 97. I am very confused on this command. From
what you had said before, the command should be used to remove or clean up
a
query if the record that is being searched for is either found or not
found,
depending on the code. So could you please check out this entire line of
code to give me a basic understanding of what it might do in this example.

If (delquery = 1) Then
DeleteQueryDef("Data Output Query")
End If

FormName = Screen.ActiveForm.Name
getval = Forms(FormName)![Client Code]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "You MUST supply a Client Code!", 48, ""
Exit Sub
Else
clcode = getval
End If

getval = Forms(FormName)![Job Code]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "You MUST supply a Job Number!", 48, ""
Exit Sub
Else
jobnum = getval
End If

getval = Forms(FormName)![Year]
If IsEmpty(getval) Or IsNull(getval) Then
MsgBox "No Year!", 48, ""
Exit Sub
Else
yearstr = getval
End If

jobtabname = "ATS JOBS " & yearstr

Querystr = "SELECT dtoutput.*, Prospect,[Client Project Number], [Data
Type],"
Querystr = Querystr & "[Data Source],[Data Rights], [Job Type],[Proc Co
Name],"
Querystr = Querystr & "[Proc Co Attn], [Acct Code], [AFE], CLIENT.[Client
Name] "
Querystr = Querystr & " From ([Data Output] as dtoutput "
Querystr = Querystr & " INNER JOIN CLIENT ON dtoutput.[Client Code] =
CLIENT.[Client Code]) "
Querystr = Querystr & " INNER JOIN [" & jobtabname
Querystr = Querystr & "] AS jobs ON (jobs.[Client Code] = dtoutput.[Client
Code])"
Querystr = Querystr & " AND (jobs.[Job Code] = dtoutput.[Job Code])"
Querystr = Querystr & " WHERE ((jobs.[Client Code] = '" & clcode
Querystr = Querystr & "') AND (jobs.[Job Code] = " & jobnum & "));"


StatusQuery.SQL = Querystr
db.QueryDefs.Append StatusQuery
StatusQuery.Close

DoCmd.OpenReport DocName, A_PREVIEW

Exit_print_Click:
Exit Sub

Err_print_Click:
MsgBox Error$
Resume Exit_print_Click

End Sub
 
K

Ken Snell [MVP]

I think I have erred.

This code line that I'd posted:
db.querydefs("Data Output Query").Delete

should be changed to this:
db.querydefs.Delete "Data Output Query"
 
E

Edward Letendre

Thanks for the upate. The new layout of the command appears to be the fix I
needed. I now have a new problem for you. I am so close to finishing this
update. Anyway, here is some code I came across this afternoon:

Forms(FormName)(SubFormName).Form![KM Count] = kmcount
If (kmcount <> 0) Then
Forms(FormName)(SubFormName).Form![Date Completed] = Format$(Now,
"dd-mmm-yy")
End If

The problem appears with the Format$ command. I am assuming that the
Format$ command came from Access 2.0 or there abouts. What I need to know is
what is the format of this command for Access 2000, as the Format$ command is
used alot in a few areas of this program. From what I can tell, this command
changes the format of data. For example, the date format is changed from the
default to the format that would be used in most databases (DD-MMM-YY). So
what can I use in place of format$ for access 2000.

Edward Letendre.
 
A

Alex White MCDBA MCSE

Hi Edward,

just try format command instead, the command reformats the output in your
case the now function giving time and date to something like 07-may-05.


--
Regards

Alex White MCDBA MCSE
http://www.intralan.co.uk

Edward Letendre said:
Thanks for the upate. The new layout of the command appears to be the fix
I
needed. I now have a new problem for you. I am so close to finishing
this
update. Anyway, here is some code I came across this afternoon:

Forms(FormName)(SubFormName).Form![KM Count] = kmcount
If (kmcount <> 0) Then
Forms(FormName)(SubFormName).Form![Date Completed] = Format$(Now,
"dd-mmm-yy")
End If

The problem appears with the Format$ command. I am assuming that the
Format$ command came from Access 2.0 or there abouts. What I need to know
is
what is the format of this command for Access 2000, as the Format$ command
is
used alot in a few areas of this program. From what I can tell, this
command
changes the format of data. For example, the date format is changed from
the
default to the format that would be used in most databases (DD-MMM-YY).
So
what can I use in place of format$ for access 2000.

Edward Letendre.

Ken Snell said:
I think I have erred.

This code line that I'd posted:
db.querydefs("Data Output Query").Delete

should be changed to this:
db.querydefs.Delete "Data Output Query"
 
E

Edward Letendre

Acutally I just tried the format function as in the following code:

Forms(FormName)(SubFormName).Form![Date Completed] = Format (Now, "dd-mmm-yy")

It does not matter if I use Format(), Format () or Format$ (), as I get the
same error, that the function Format does not exist in the library. What am
I doing wrong?

Edward Letendre.


Alex White MCDBA MCSE said:
Hi Edward,

just try format command instead, the command reformats the output in your
case the now function giving time and date to something like 07-may-05.


--
Regards

Alex White MCDBA MCSE
http://www.intralan.co.uk

Edward Letendre said:
Thanks for the upate. The new layout of the command appears to be the fix
I
needed. I now have a new problem for you. I am so close to finishing
this
update. Anyway, here is some code I came across this afternoon:

Forms(FormName)(SubFormName).Form![KM Count] = kmcount
If (kmcount <> 0) Then
Forms(FormName)(SubFormName).Form![Date Completed] = Format$(Now,
"dd-mmm-yy")
End If

The problem appears with the Format$ command. I am assuming that the
Format$ command came from Access 2.0 or there abouts. What I need to know
is
what is the format of this command for Access 2000, as the Format$ command
is
used alot in a few areas of this program. From what I can tell, this
command
changes the format of data. For example, the date format is changed from
the
default to the format that would be used in most databases (DD-MMM-YY).
So
what can I use in place of format$ for access 2000.

Edward Letendre.

Ken Snell said:
I think I have erred.

This code line that I'd posted:
db.querydefs("Data Output Query").Delete

should be changed to this:
db.querydefs.Delete "Data Output Query"

--

Ken Snell
<MS ACCESS MVP>

message Again, I want to thank you for your help up to this point. But I have
another problem. When I add the following code my access application
(to
be
percise to the code behind a form), as show below:

db.querydefs("Data Output Query").Delete

as shown in the code you saw before in this post, I get the following
error
message about the .Delete part of the command above:

compile error: method or data member (Delete or .Delete) not found.

The weird thing is that this error does not occur in the other 20 to 30
or
so places that I have used this code to make the changes in the past to
the
new delete method for the querydefs. Why would this delete method
cause
an
error at this point and not in other parts of the application with the
same
code? This is very weird in deed.

Edward Letendre.


:

The following code block is what I would change:

If db.QueryDefs.Count > 0 Then
For I = 0 To db.QueryDefs.Count - 1
' MsgBox db.QueryDefs(I).Name, 48, ""
If (db.QueryDefs(I).Name = "Data Output Query") Then
delquery = 1
End If
Next I
End If

If (delquery = 1) Then
DeleteQueryDef("Data Output Query")
End If


I would change it to this:

If db.QueryDefs.Count > 0 Then
For I = db.QueryDefs.Count - 1 To 0 Step -1
If (db.QueryDefs(I).Name = "Data Output Query") Then
db.QueryDefs(I).Delete
Exit For
End If
Next I
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top