469,307 Members | 2,435 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,307 developers. It's quick & easy.

GetLineNumberFunction() is not working

3
Hi,

Good Day!

I have a form with a subform and I want to have a line number for each of the records that will be displayed.

Here is my setup:


I have a table with the field "ID" with the setup "Integer".

I have a module called GetLNumber and put this code

Expand|Select|Wrap|Line Numbers
  1. Function GetLineNumber(F As Form, KeyName As String, KeyValue)
  2.  
  3. Dim RS As DAO.Recordset
  4. Dim CountLines
  5.  
  6. On Error GoTo Err_GetLineNumber
  7.  
  8. Set RS = F.RecordsetClone
  9.  
  10. ' Find the current record.
  11. Select Case RS.Fields(KeyName).Type
  12. ' Find using numeric data type key value.
  13. Case dbInteger, dbLong, dbCurrency, dbSingle, dbDouble, dbByte
  14. RS.FindFirst "[" & KeyName & "] = " & KeyValue
  15. ' Find using date data type key value.
  16. Case dbDate
  17. RS.FindFirst "[" & KeyName & "] = #" & KeyValue & "#"
  18. ' Find using text data type key value.
  19. Case dbText
  20. RS.FindFirst "[" & KeyName & "] = '" & KeyValue & "'"
  21. Case Else
  22. MsgBox "ERROR: Invalid key field data type!"
  23. Exit Function
  24. End Select
  25.  
  26. ' Loop backward, counting the lines.
  27. Do Until RS.BOF
  28. CountLines = CountLines + 1
  29. RS.MovePrevious
  30. Loop
  31.  
  32. Bye_GetLineNumber:
  33. ' Return the result.
  34. GetLineNumber = CountLines
  35.  
  36. Exit Function
  37.  
  38. Err_GetLineNumber:
  39. CountLines = 0
  40. Resume Bye_GetLineNumber
  41. End Function
and then in the textbox called txtID has a Control Source =GetLineNumber([Forms]![frmEarlyWarningSystem].[Form].[frmEarlyWarningSystemSubform],"ID",[ID])

but when I click Form View, the field for txtID contains #Type!

I tried this code also but still no luck.

Expand|Select|Wrap|Line Numbers
  1. Function GetLineNumber(F As Form, KeyName As String, KeyValue)
  2. Dim RS As Object
  3. Dim CountLines
  4.  
  5. On Error GoTo Err_GetLineNumber
  6.  
  7. Set RS = F.Recordset.Clone
  8.  
  9. RS.Find "[" & KeyName & "] = " & KeyValue
  10.  
  11. ' Loop backward, counting the lines.
  12. Do Until RS.BOF
  13. CountLines = CountLines + 1
  14. RS.MovePrevious
  15. Loop
  16.  
  17. Bye_GetLineNumber:
  18. ' Return the result.
  19. GetLineNumber = CountLines
  20.  
  21. Exit Function
  22.  
  23. Err_GetLineNumber:
  24. CountLines = 0
  25. Resume Bye_GetLineNumber
  26.  
  27. End Function
what did I missed?

Your help is very much appreciated!

tia
Apr 9 '13 #1
3 1087
NeoPa
32,173 Expert Mod 16PB
If it allows you to pass a form reference then you could try :
Expand|Select|Wrap|Line Numbers
  1. =GetLineNumber([Forms]![frmEarlyWarningSystem]![frmEarlyWarningSystemSubform]![Form],"ID",[ID])
Your reference to the subform object was incorrect (wrong order of elements) and SQL doesn't recognise dots (.), so this is worth a try.

See Referring to Items on a Sub-Form for correct structure of references from VBA.
Apr 9 '13 #2
blur
3
Thanks NeoPa for your quick response. I've tried it but still not working. it displays "#Name?"

here is the new value of control source of my textbox

Expand|Select|Wrap|Line Numbers
  1. =GetLineNumber([Forms]![frmEarlyWarningSystem]![Form]![frmEarlyWarningSystemSubform]![Form],"ID",[ID])
Apr 9 '13 #3
NeoPa
32,173 Expert Mod 16PB
That won't work. If you look at the linked article you'll clearly see why.

Expand|Select|Wrap|Line Numbers
  1. [Forms]![frmEarlyWarningSystem]
Is a reference to a form object. Form objects don't have Form properties (After all - what would be the point?).

Subform controls, however, are not Form objects at all, but contain a Form. That is why it makes sense that they would have a Form property.

It's all in the linked article.

You may well find, however, that there are limits to what you can refer to at all from within SQL. That's not something I can help with I'm afraid.
Apr 10 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Gary | last post: by
6 posts views Thread by Mullin Yu | last post: by
8 posts views Thread by Hardy Wang | last post: by
5 posts views Thread by Martin Heuckeroth | last post: by
5 posts views Thread by tshad | last post: by
8 posts views Thread by jojobar | last post: by
2 posts views Thread by Don | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.