VBA question

Started by
4 comments, last by Crispy 21 years, 8 months ago
Hi, I need to check if a record with any given characteristics already exists in the Access database. How should I go about that (what function to use?). Here''s the specific case: The user adds a new entry with Month = 2, Year = 2002 Now these cannot overlap on any two records, but since I don''t know how to or if it is at all possible to create a two-field unique id for a table, I''m sticking with VBA code So I need to conduct a find in the table for any other records with Month = 2 and Year = 2002. How can I do that? Thanks in advance, Crispy
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Advertisement
Im not sure, but I think you want some sql?
anyway:


  SELECT * WHERE month = 2 AND year = 2002  


anyway just check if you get anything with EOF (i.e. if first record is EOF then you didnt) and your done.
Mkay...

So I select stuff from the database by calling:

      Dim db As Database    Dim strSQL As String        strSQL = "SELECT * FROM MyTable WHERE Year = Me!Year AND MonthID = Me!Month"    Set db = CurrentDb        db.Execute strSQL  


Now, I''m quite useless all by myself when in comes to coding in VB because of some serious lack of experience, so I can''t figure out how to store what db.Execute returns in some variable. var = db.Execute strSQL won''t work... Furthermore, I have no idea as to how to iterate through the returned records. I''d appreciate some more specific pointers if possible...

Crispy
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
to search on the net or visit vbcode.com

in short I work with access95 because the company i work for are cheap.

I think you are missing this

Dim rst As Recordset

then sonething like this


Set rst = strSQL

should get you going in the right direction!!
Thanks Themonkster, you led me on the right track.

Here''s my solution if it''s of any use to anyone:

      Dim rst As DAO.Recordset    Set rst = Me.RecordsetClone        Set rst = CurrentDb.OpenRecordset("SELECT * FROM MyTable WHERE [Year] = " & _              Me!CurrentYear.Value & " AND [Month] = " & Me!CurrentMonth.Value & "", dbOpenDynaset)                  If rst.RecordCount > 1 Then        MsgBox "The record exists!"    End If            rst.Close  


"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
just put a UNIQUE INDEX on that field (it will not allow any two records to have the same value)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])

This topic is closed to new replies.

Advertisement