Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

auto number in a table

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I refer to the wonderful article called Q262099 - ACC2000 Filter-by-Form Example by by Getz, Litwin, and Reddick (Sybex),' Copyright 1994 - 1997. All rights reserved, written for the table tblclients. It works excellent when the ClientD is a unique 5 letters word. However my ClientID is an unique auto number, not a Text, and I cannot use this fine function.Can anybody help me to modify the function,?
=========
Option Compare Database 'Use database order for string comparisons
Option Explicit

' Adapted from Microsoft Access 2 Developer's Handbook
' by Getz, Litwin, and Reddick (Sybex)
' Copyright 1994 - 1997. All rights reserved.

' REQUIRES A REFERENCE TO Microsoft DAO 3.6.

Const adhcSeparator = ";"
Const adhcAssignment = "="

Function adhDeleteItem(ByVal varInfo As Variant, ByVal varItemName As Variant) As Variant

' Delete a specific item name and its value.
'
' Return the new info string, with the requested
' item and its value removed.
'
' Adapted from Microsoft Access 2 Developer's Handbook
' by Getz, Litwin, and Reddick (Sybex)
' Copyright 1994 - 1997. All rights reserved.
'
' In:
' varInfo: string of items, delimited with adhcSeparator
' varItemName: name of item to delete from varInfo
'
' Out:
' Return value: varInfo, with the item and its value deleted.
'
' Example:
' If varInfo is "x=5;y=7;z=12;", calling
' adhDeleteItem(varInfo, "y")
' will return "x=5;z=12;"

Dim lngEndPos As Long
Dim strLeftPart As String
Dim lngPos As Long
Dim intRet As Integer

On Error GoTo adhDeleteItem_Err

' Look for the tag that you've asked to delete.
lngPos = adhFindItemPos(varInfo, varItemName)

If lngPos > 0 Then
' Find the end of the requested tag value. This'll be
' 0 if there's no more items after this one.
lngEndPos = InStr(lngPos + 1, varInfo, adhcSeparator)

' Gather up the part of the tag string to the left of the
' requested tag. This can't fail, since you wouldn't be
' here if lngPos wasn't greater than 0.
strLeftPart = Left$(varInfo, lngPos - 1)

' If there's stuff to the right of the requested item, tack it
' onto the end of the info string.
If lngEndPos > 0 Then
varInfo = strLeftPart & Mid$(varInfo, lngEndPos + 1)
End If
End If
adhDeleteItem = varInfo

adhDeleteItem_Exit:
Exit Function

adhDeleteItem_Err:
Select Case Err.Number
Case Else
adhErrorHandler Err.Number, Err.Description, "adhDeleteItem"
End Select
Resume adhDeleteItem_Exit

End Function

Sub adhErrorHandler(intErr As Integer, strError As String, strProc As String)

' Adapted from Microsoft Access 2 Developer's Handbook
' by Getz, Litwin, and Reddick (Sybex)
' Copyright 1994 - 1997. All rights reserved.

MsgBox "Error: " & strError & " (" & intErr & ")", _
vbInformation, strProc
End Sub

Private Function adhFindItemPos(varInfo As Variant, varItemName As Variant) As Long

' Adapted from Microsoft Access 2 Developer's Handbook
' by Getz, Litwin, and Reddick (Sybex)
' Copyright 1994 - 1997. All rights reserved.

Dim lngPos As Long
Dim intRet As Integer

On Error GoTo adhFindItemPos_Err
lngPos = 0

' Don't even bother if the info string or the item name is null
' Use a little trick here to trap both the Null case and the
' zero-length string case: if Len(yourString & vbNullString) = 0, then
' <yourString> is either Null or a ZLS.
If Len(varInfo &amp; vbNullString) > 0 And Len(varItemName &amp; vbNullString) > 0 Then
' Stick a adhcSeparator on the front, and then look for
' &quot;;varItemName=&quot;
' If it's there, it'll find it on the first pass. No loops!
' This code must be fast, since it gets called A LOT!
lngPos = InStr(adhcSeparator &amp; varInfo, adhcSeparator &amp; varItemName &amp; adhcAssignment)
End If

adhFindItemPos = lngPos

adhFindItemPos_Exit:
Exit Function

adhFindItemPos_Err:
Select Case Err.Number
Case Else
adhErrorHandler Err.Number, Err.Description, &quot;adhFindItemPos&quot;
End Select
Resume adhFindItemPos_Exit

End Function

Function adhGetItem(ByVal varInfo As Variant, ByVal varItemName As Variant) As Variant

' Retrieve a specific item value.
' This function will either return the requested
' value or Null if the item name wasn't found.

' Adapted from Microsoft Access 2 Developer's Handbook
' by Getz, Litwin, and Reddick (Sybex)
' Copyright 1994 - 1997. All rights reserved.
'
' In:
' varInfo: string of items, delimited with adhcSeparator
' varItemName: name of item to retrieve from varInfo
'
' Out:
' Return value: the value associated with varItemName.
'
' Example:
' If varInfo is &quot;x=5;y=7;z=12;&quot;, calling
' adhGetItem(varInfo, &quot;y&quot;)
' will return 7
'

Dim lngPos As Long
Dim lngEndPos As Long
Dim varResult As Variant
Dim intRet As Integer

On Error GoTo adhGetItem_Err

varResult = Null
lngPos = adhFindItemPos(varInfo, varItemName)
' If the item was found, keep a'goin'.
If lngPos > 0 Then
' Move lngPos to the start of the item value, and
' lngEndPos to the next adhcSeparator, if there is one.
lngPos = lngPos + Len(varItemName) + Len(adhcAssignment)
lngEndPos = InStr(lngPos, varInfo, adhcSeparator)

' Interpret a zero-length property as Null
If lngEndPos = lngPos Then
varResult = Null
Else
' If there wasn't a adhcSeparator, just use the rest
' of the info string. Otherwise, take the part between
' lngPos and lngEndPos.
If lngEndPos = 0 Then
varResult = Mid$(varInfo, lngPos)
Else
varResult = Mid$(varInfo, lngPos, lngEndPos - lngPos)
End If
End If
End If
adhGetItem = varResult

adhGetItem_Exit:
Exit Function

adhGetItem_Err:
Select Case Err.Number
Case Else
adhErrorHandler Err.Number, Err.Description, &quot;adhGetItem&quot;
End Select
Resume adhGetItem_Exit

End Function

Function adhPutItem(ByVal varInfo As Variant, ByVal varItemName As Variant, ByVal varItemValue As Variant) As Variant

' Append the value
'
' [varItemName]=[varItemValue];
'
' onto the varInfo value passed in. If the
' item name already exists, it is deleted first and then the new
' value is appended to the end.

' Adapted from Microsoft Access 2 Developer's Handbook
' by Getz, Litwin, and Reddick (Sybex)
' Copyright 1994 - 1997. All rights reserved.
'
' In:
' varInfo: string of items, delimited with adhcSeparator
' varItemName: name of item to place into varInfo
' varItemValue: value of item
' Out:
' Return value: modified value of varInfo.
'
' Example:
' If varInfo is &quot;x=5;y=7;z=12;&quot;, calling
' adhPutItem(varInfo, &quot;q&quot;, &quot;15&quot;)
' will return &quot;x=5;y=7;z=12;q=15;&quot;
'
On Error GoTo adhPutItem_Err
Dim intRet As Integer

' If there's already a value in the info string for the item
' you're trying to replace, just REMOVE it.

varInfo = adhDeleteItem(varInfo, varItemName)

' By passing in a null or ZLS for the strItemValue, you effectively
' delete the tag.
If Len(varItemValue &amp; vbNullString) > 0 Then
varInfo = varInfo &amp; varItemName &amp; adhcAssignment &amp; varItemValue &amp; adhcSeparator
End If
adhPutItem = varInfo

adhPutItem_Exit:
Exit Function

adhPutItem_Err:
Select Case Err.Number
Case Else
adhErrorHandler Err.Number, Err.Description, &quot;adhPutItem&quot;
End Select
Resume adhPutItem_Exit

End Function

Function adhCtlTagDeleteItem(ctl As Control, ByVal varItemName As Variant)

' Delete a specific tag name and its value from the
' requested control's .Tag property.
'
' Adapted from Microsoft Access 2 Developer's Handbook
' by Getz, Litwin, and Reddick (Sybex)
' Copyright 1994 - 1997. All rights reserved.
'
' In:
' ctl: reference to a control
' varItemName: name of item to delete
' Out:
' Return Value: the control's Tag property, with the item deleted.
' See adhDeleteItem for details.

ctl.Tag = adhDeleteItem(ctl.Tag, varItemName)
adhCtlTagDeleteItem = ctl.Tag
End Function

Function adhCtlTagGetItem(ctl As Control, ByVal varItemName As Variant) As Variant

' Retrieve a specific tag name from the requested control's
' .Tag property. This function will either return the requested
' value or Null if the tag name wasn't found.

' Adapted from Microsoft Access 2 Developer's Handbook
' by Getz, Litwin, and Reddick (Sybex)
' Copyright 1994 - 1997. All rights reserved.
'

adhCtlTagGetItem = adhGetItem(ctl.Tag, varItemName)
End Function

Function adhCtlTagPutItem(ctl As Control, ByVal varItemName As Variant, ByVal varItemValue As Variant) As Integer

' Append the value
'
' [varItemName]=[varItemValue]
'
' onto the .Tag property for the requested control.
' See adhPutItem() for more information.

' Adapted from Microsoft Access 2 Developer's Handbook
' by Getz, Litwin, and Reddick (Sybex)
' Copyright 1994 - 1997. All rights reserved.

Const adhcErrTagTooLong = 2176

Dim varOldTag As Variant

On Error GoTo CtlTagPutItemErr

' Assign the new tag value and then return True.
varOldTag = ctl.Tag
ctl.Tag = adhPutItem(varOldTag, varItemName, varItemValue)
adhCtlTagPutItem = True

ctlTagPutItemExit:
Exit Function

CtlTagPutItemErr:
If Err.Number = adhcErrTagTooLong Then
' Make sure ctl.Tag hasn't changed. Then return False.
ctl.Tag = varOldTag
Else
adhErrorHandler Err.Description, Err.Number, &quot;adhCtlTagPutItem&quot;
End If
adhCtlTagPutItem = False
Resume ctlTagPutItemExit
End Function
Option Compare Database 'Use database order for string comparisons
Option Explicit

' REQUIRES A REFERENCE TO Microsoft DAO 3.6.

Const QUOTE = &quot;&quot;&quot;&quot;

' This string is the text that gets appended
' to the chosen form name, once it's become a
' QBF form. It's completely arbitrary, and can be
' anything you like.
Public Const conQBFSuffix = &quot;_QBF&quot;

Private Function BuildSQLString( _
ByVal strFieldName As String, _
ByVal varFieldValue As Variant, _
ByVal intFieldType As Integer)

' Build string that can be used as part of an
' SQL WHERE clause. This function looks at
' the field type for the specified table field,
' and constructs the expression accordingly.

Dim strTemp As String

On Error GoTo HandleErrors

If Left$(strFieldName, 1) <> &quot;[&quot; Then
strTemp = &quot;[&quot; &amp; strFieldName &amp; &quot;]&quot;
End If

' If the first part of the value indicates that it's
' to be left as is, leave it alone. Otherwise,
' munge the value as necessary.
If IsOperator(varFieldValue) Then
strTemp = strTemp &amp; &quot; &quot; &amp; varFieldValue
Else
' One could use the BuildCriteria method here,
' but it's not as flexible as I'd like to
' be. So, this code does all the work manually.

Select Case intFieldType
Case dbBoolean
' Convert to TRUE/FALSE
strTemp = strTemp &amp; &quot; = &quot; &amp; CInt(varFieldValue)
Case dbText, dbMemo
' Assume we're looking for anything that STARTS with the text we got.
' This is probably a LOT slower. If you want direct matches
' instead, use the commented-out line.
' strTemp = strTemp &amp; &quot; = &quot; &amp; QUOTE &amp; varFieldValue &amp; QUOTE
strTemp = strTemp &amp; &quot; LIKE &quot; &amp; QUOTE &amp; varFieldValue &amp; &quot;*&quot; &amp; QUOTE
Case dbByte, dbInteger, dbLong, dbCurrency, dbSingle, dbDouble
' Convert to straight numeric representation.
strTemp = strTemp &amp; &quot; = &quot; &amp; varFieldValue
Case dbDate
' Convert to #date# format.
strTemp = strTemp &amp; &quot; = &quot; &amp; &quot;#&quot; &amp; varFieldValue &amp; &quot;#&quot;
Case Else
' This function really can't handle any of the other data types. You can
' add more types, if you care to handle them.
strTemp = vbNullString
End Select
End If
BuildSQLString = strTemp

ExitHere:
Exit Function

HandleErrors:
MsgBox &quot;Error: &quot; &amp; Err.Description &amp; &quot; (&quot; &amp; Err.Number &amp; &quot;)&quot;, vbExclamation, &quot;BuildSQLString&quot;
strTemp = vbNullString
Resume ExitHere
End Function

Private Function BuildWHEREClause(frm As Form) As String

' Build the full WHERE clause based on fields
' on the passed-in form. This function attempts
' to look at all controls that have the correct
' settings in the Tag properties.

Dim strLocalSQL As String
Dim strTemp As String
Dim varDataType As Integer
Dim varControlSource As Variant
Dim ctl As Control

Const conAND As String = &quot; AND &quot;

For Each ctl In frm.Controls
' Get the original control source.
varControlSource = adhCtlTagGetItem(ctl, &quot;qbfField&quot;)
If Not IsNull(varControlSource) Then
' If the value of the control isn't null...
If Not IsNull(ctl) Then
' then get the value.
varDataType = adhCtlTagGetItem(ctl, &quot;qbfType&quot;)
If Not IsNull(varDataType) Then
strTemp = &quot;(&quot; &amp; BuildSQLString(varControlSource, ctl, varDataType) &amp; &quot;)&quot;
strLocalSQL = strLocalSQL &amp; conAND &amp; strTemp
End If
End If
End If
Next ctl

' Trim off the leading &quot; AND &quot;
If Len(strLocalSQL) > 0 Then
BuildWHEREClause = &quot;(&quot; &amp; Mid$(strLocalSQL, Len(conAND) + 1) &amp; &quot;)&quot;
End If
End Function

Public Function DoQBF(ByVal strFormName As String, _
Optional blnCloseIt As Boolean = True) As String

' Load the specified form as a QBF form. If
' the form is still loaded when control returns
' to this function, then it will attempt to
' build an SQL WHERE clause describing the
' values in the fields. DoQBF() will return
' either that SQL string or an empty string,
' depending on what the user chose to do and
' whether or not any fields were filled in.

' In:
' strFormName: Name of the form to load
' blnCloseIt: Close the form, if the user didn't?
' Out:
' Return Value: The calculated SQL string.

Dim strSQL As String

DoCmd.OpenForm strFormName, WindowMode:=acDialog

' You won't get here until user hides or closes the form.
' If the user closed the form, there's nothing
' to be done. Otherwise, build up the SQL WHERE
' clause. Once you're done, if the caller requested
' the QBF form to be closed, close it now.
If IsFormLoaded(strFormName) Then
strSQL = BuildWHEREClause(Forms(strFormName))
If blnCloseIt Then
DoCmd.Close acForm, strFormName
End If
End If
DoQBF = strSQL
End Function

Public Function QBFDoClose()
' This is a function so it can be called easily
' from the Properties window directly.

' Close the current form.
On Error Resume Next
DoCmd.Close
End Function

Public Function QBFDoHide(frm As Form)
' This is a function so it can be called easily
' from the Properties window directly.

Dim strSQL As String
Dim strParent As String

'Get the name of the Parent form
strParent = adhGetItem(frm.Tag, &quot;Parent&quot;) &amp; vbNullString

'Create the appropriate WHERE clause based on the fields with data in them.
strSQL = DoQBF(frm.Name, False)

If Len(strParent) > 0 Then
'Open the Parent form filtered with the Where clause generated above
DoCmd.OpenForm FormName:=strParent, View:=acNormal, WhereCondition:=strSQL
End If

'Make this QBF form invisible.
frm.Visible = False
End Function

Private Function IsFormLoaded(strName As String) As Boolean

' Return a logical value indicating whether a
' given formname is loaded or not.
' You could use the IsLoaded property of a member
' of the AllForms collection to get this information, but
' that method raises an error if you ask about a
' for that doesn't exist. The obscure SysCmd function
' does not.
On Error Resume Next
IsFormLoaded = (SysCmd(acSysCmdGetObjectState, acForm, strName) <> 0)
End Function

Private Function IsOperator(varValue As Variant) As Boolean

' Return a logical value indicating whether a
' value passed in is an operator or not.
' This is NOT infallible, and may need correcting.

Dim strTemp As String

strTemp = Trim$(UCase(varValue))
IsOperator = False

' Check first character for <,>, or =
If InStr(1, &quot;<>=&quot;, Left$(strTemp, 1)) > 0 Then
IsOperator = True
' Check for IN (x,y,z)
ElseIf ((Left$(strTemp, 4) = &quot;IN (&quot;) And (Right$(strTemp, 1) = &quot;)&quot;)) Then
IsOperator = True
' Check for BETWEEN ... AND ...
ElseIf ((Left$(strTemp, 8) = &quot;BETWEEN &quot;) And (InStr(1, strTemp, &quot; AND &quot;) > 0)) Then
IsOperator = True
' Check for NOT xxx
ElseIf (Left$(strTemp, 4) = &quot;NOT &quot;) Then
IsOperator = True
' Check for LIKE xxx
ElseIf (Left$(strTemp, 5) = &quot;LIKE &quot;) Then
IsOperator = True
End If
End Function

 
Sorry, but you have a bunch of functions posted; which are bombing out and what line are they bombing on??

Kathryn


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top