View Full Version : A Tricky Validation Macro


moel
09-05-2005, 06:23 AM
Hi

I'm currently writing a database that is designed for storing radio frequencies, and I need a macro that will check the results of a query to make sure that they are not within 2khz of each other. Problem is i've never ventured outside the things that a macro can do as standard and the expression builder is starting to irk me something rotten.

Basically i require an expression that will compare a list of frequencies, and then throw up a dialog box if within that list of frequencies there are 2 or more that are too close together...I would really appreciate some help on this one..i know i'm a newbie but please please please HELP!
:(
Feel free to get in touch by any means you want

Sergeant
09-05-2005, 08:55 AM
How you approach this depends largely upon what you want to know about the <2KHz spaced freqs and what you then want to do about it. But, going along with what you asked for...

Ensure your query is sorted by freq, ascending.
Make a form and set your freq query as the data source.
Put a button on the form and place this code in the "onclick" event of the button:
Private Sub CommandButton1_Click()
Dim rst As Recordset
Dim tempfreq As Double
Dim cnt As Integer
cnt = 0
tempfreq = 0
Set rst = Me.RecordsetClone
rst.MoveFirst
Do While Not rst.EOF
If tempfreq > 0 Then

If (rst("Freq") - tempfreq) < 2000 Then
cnt = cnt + 1
End If
End If
tempfreq = rst("Freq")
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
If cnt > 0 Then
MsgBox "The query returned " & cnt & " instances where frequency spacing is < 2KHz"
End If
End Sub

moel
09-05-2005, 09:54 AM
Thank you so much sergeant i shall try this when i get to work tomorrow morning, but hopefully it should work :) cheers for your help and i may call on u 2morrow for some more help...but i'll spend a few hours trying to work it out myself to start off with..cheers and ttfn