Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Queries / November 2005

Tip: Looking for answers? Try searching our database.

Help with a comparison code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mekinnik - 06 Nov 2005 06:03 GMT
I have this code I got to lookupfrom the MSDS field the highest number
already used for a left 2 letter prefix, then its suppose to add 1 to the
prefix only it doesn't work.  The MSDS field prefix is based on the users
selection from another field. Can someone explain why??

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim varResult As Variant
Dim strWhere As String
Dim iNum As Integer
Dim strFirstChar As String
Dim strVal As String
   Me.Dept.SetFocus
   strVal = Me.Dept.SelText
   strFirstChar = Left(strVal, 2)
   strVal = strFirstChar
   
If Me.NewRecord Then
   'Lookup the highest number already used for the prefix.
   strWhere = "MSDS Like " "" & Me.[Dept] & " * """"
   varResult = DMax("MSDS", "Hazinventory", strWhere)
If Not IsNull(varResult) Then
   iNum = Val(Right(varResult, 2)) + 1
End If

   'Assign the Value
Me.MSDS = strVal & Format(iNum, "00")
End If
End Sub
Randy Harris - 06 Nov 2005 06:23 GMT
> I have this code I got to lookupfrom the MSDS field the highest number
> already used for a left 2 letter prefix, then its suppose to add 1 to the
[quoted text clipped - 24 lines]
> End If
> End Sub

It looks to me as though MSDS is a text field.  DMAX of a text field will
return the last value sorted in alphabetic order, not a numeric value. You
could probably use DMAX("Mid([MSDS], 3)", ...   to get the highest numeric
value with the first two characters stripped off.

This is one of the reasons for normalizing data. You shouldn't have the
prefix and the value together in the same field.
John Spencer - 06 Nov 2005 13:40 GMT
I suspect that strWhere is not returning what you think it is.

  strWhere = "MSDS Like " "" & Me.[Dept] & " * """"

StrWhere is now set to
  MSDS Like " LL * "  (Note the leading and trailing spaces)
where LL is being used to indicate two letters.  I think you want
  MSDS Like "LL*"

For clarity, I prefer to use the following method.
 strWhere = "MSDS LIKE " & chr(34) & me.[Dept] & Chr(34)

But you can use
 strWhere = "MSDS Like """ & Me.[Dept] & "*""""

And further review of your code leads me to guess that you don't want Me.Dept in
that string, but strFirstChar.  Also, I would simplify this by not setting focus
to Me.Dept and simply use strVal = me.Dept (or Me.Dept.Value).

 StrVal=Left(Me.Dept,2)
 strWhere = "MSDS Like """ & strVal & "*""""
 varResult = DMax("MSDS", "Hazinventory", strWhere)

In addition, what is going to happen when your values exceed 99?

> I have this code I got to lookupfrom the MSDS field the highest number
> already used for a left 2 letter prefix, then its suppose to add 1 to the
[quoted text clipped - 28 lines]
> Message posted via AccessMonster.com
> http://www.accessmonster.com/Uwe/Forums.aspx/access-queries/200511/1
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.