Re: Refer to the first row of the table
- From: "Dirk Goldgar" <dg@xxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 16 Aug 2007 16:14:13 -0400
In 76cb49043acd7@uwe">news:76cb49043acd7@uwe,
samotek via AccessMonster.com <u15330@uwe> wrote:
Thank you so much indeed. You are right in what you guesed,i just
didnt see that i have no field named "WhenCreated" and i thought this
is osmehting i Access i do not know.Now i have corrected it and it is
OK, i get no error ? Now i tried to build and sql in the smae code,
trying todelete all the afids which are not equal to FirstAfid, but i
get the error Syntax error in query expression "TblClients1.afid Not
FirstAfid".
Could yo help me correct the code ? My whole function is the
following : Public Function STR()
Dim FirstAfid As Long
Dim StrSQL As String
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT TOP 1 afid FROM
tblClients1 ORDER BY afid")
With rs
If .EOF Then
MsgBox "No records!"
ElseIf IsNull(!afid) Then
MsgBox "No afid in first record!"
Else
FirstAfid = !afid
End If
.Close
End With
StrSQL = " DELETE TblClients1.*, TblClients1.afid FROM TblClients1
WHERE TblClients1.afid NOT FirstAfid"
CurrentDb.Execute StrSQL
End Function
=========================
Syntax error in query expression "TblClients1.afid Not FirstAfid"
Although your SQL statement could be simplified, there are two main
errors in it. First, you need to use the "<>" ("not equal") operator
instead of just "NOT". Second, you need to embed the *value* of
FirstAfid in the SQL string, not the variable name. Aside from those
errors, remember that you don't want to do this unless you actually
found a value for FirstAfid. So I would amend your code like this:
'---- start of revised section of code ----
With rs
If .EOF Then
MsgBox "No records!"
ElseIf IsNull(!afid) Then
MsgBox "No afid in first record!"
Else
FirstAfid = !afid
StrSQL = _
"DELETE * FROM TblClients1 WHERE afid <> " & FirstAfid
CurrentDb.Execute StrSQL, dbFailOnError
End If
.Close
End With
'---- end of revised section of code ----
Now that you've made it clear that you want to order by afid, and only
want to keep the records with the lowest afid, the whole process could
be simplified to a single delete query:
DELETE * FROM TblClients1
WHERE afid >
(SELECT Min(T.afid) FROM TblClients1 As T)
That statement uses a subquery to find out the lowest afid value, and
then deletes all records with afid greater than that value. I haven't
tested the SQL, but something along those lines ought to work.
--
Dirk Goldgar, MS Access MVP
www.datagnostics.com
(please reply to the newsgroup)
.
- Follow-Ups:
- Re: Refer to the first row of the table
- From: samotek via AccessMonster.com
- Re: Refer to the first row of the table
- References:
- Refer to the first row of the table
- From: samotek via AccessMonster.com
- Re: Refer to the first row of the table
- From: Dirk Goldgar
- Re: Refer to the first row of the table
- From: samotek via AccessMonster.com
- Re: Refer to the first row of the table
- From: Dirk Goldgar
- Re: Refer to the first row of the table
- From: samotek via AccessMonster.com
- Refer to the first row of the table
- Prev by Date: Re: Refer to the first row of the table
- Next by Date: Re: "Bold" text field on a report
- Previous by thread: Re: Refer to the first row of the table
- Next by thread: Re: Refer to the first row of the table
- Index(es):
Relevant Pages
|
Loading