473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Create Next ID from Table

83
I only want the assID to report the highest number if the case number is in the same month and year, otherwise I want it to assign 0. For some reason, my code isn't working.
The lsi case number should read something like 407-112
Lets say that in the month of April of 07 the highest case number we had was 112, then the assID should report 112, this way, when I assign the LSI Case Number for a new case in the month of april it will read 407-113.

I also have another problem because in the criteria part of the DMAX statement I am looking at the LEFT most 3 variables, it could be 4 if we are in month 10 11 or 12.

So, I want the dmax function to scan my database for the month and year formatted as myy (could be a 2 number long month) and report back the largest right most number (in the example above it would be 112, but if it was a new month it would find no number. The right most part of the LSI Case number is always 3 digits long.
After that I want to assign the newest case to be exactly as it is written in my code.

Expand|Select|Wrap|Line Numbers
  1. Private Sub LSI_Case_Number_DblClick(Cancel As Integer)
  2. Dim assID As Long
  3.    assID = DMax("Right([LSI Case Number],3)", "Master Log", Left([LSI Case Number], 3) = Format(Now, "m") & Format(Now, "yy"))
  4.    Me.LSI_Case_Number = Format(Now, "m") & Format(Now, "yy") & "-" & Format(assID + 1, "000")
  5. End Sub
  6.  
Apr 10 '07 #1
24 2835
MMcCarthy
14,534 Expert Mod 8TB
Try this ...

Expand|Select|Wrap|Line Numbers
  1. Private Sub LSI_Case_Number_DblClick(Cancel As Integer)
  2. Dim assID As Long
  3.    assID = DMax("CLng(Right([LSI Case Number],3))", "Master Log", Left([LSI Case Number], InStr([LSI Case Number], "-")-1) = CStr(Month(Now())) & CStr(Year(Now())))
  4.    Me.LSI_Case_Number = CStr(Month(Now())) & CStr(Year(Now())) & "-" & Format(assID + 1, "000")
  5. End Sub
  6.  
Mary
Apr 11 '07 #2
NeoPa
32,556 Expert Mod 16PB
Firstly, I would strongly suggest that you change the format of your [LSI Case Number] field to yyyymm-nnn.
What you have at the moment will certainly complicate matters for you and almost certainly will be harder to work with and understand for anyone trying to use it. It will also appear in a funny and unpredictable order in your tables. A two digit year is probably usable at this stage of the century, but is not a wise choice nevertheless.

Assuming my recommendations are followed, this code should help :
Expand|Select|Wrap|Line Numbers
  1. Private Sub LSI_Case_Number_DblClick(Cancel As Integer)
  2.   Dim strAssID As String
  3.  
  4.   strAssID = NZ(DMax("Right([LSI Case Number],3)", _
  5.                      "Master Log", _
  6.                      "[LSI Case Number] Like Format(Date(),'yyyymm')"), "0")
  7.   Me.LSI_Case_Number = Format(Date(),"yyyymm\-") & _
  8.                        Format(CLng(strAssID) + 1, "000")
  9. End Sub
Apr 11 '07 #3
pks00
280 Expert 100+
What about using a recordset to handle it. Might also be better in multi user environments to handle locking issues. Just a thought

eg


assID = DMax("Right([LSI Case Number],3)", "Master Log", Left([LSI Case Number], 3) = Format(Now, "m") & Format(Now, "yy"))
Me.LSI_Case_Number = Format(Now, "m") & Format(Now, "yy") & "-" & Format(assID + 1, "000")
End Sub

Expand|Select|Wrap|Line Numbers
  1. dim rs as dao.recordset
  2. dim sSql as string
  3.  
  4. sSql = "SELECT MAX(  MID$([LSI Case Number], Instr(1,[LSI Case Number],'-')+1) AS MaxCaseNo FROM [Master Log] WHERE Left$([LSI Case Number],Instr(1,[LSI Case Number],'-')-1) = " & Format(Now(),"MYY")
  5.  
  6. set rs=currentdb.openrecordset(sSql)
  7. if rs.eof = true then
  8.     Me.LSI_Case_Number = Format(Now(),"MYY") & "-001"
  9. else
  10.     Me.LSI_Case_Number = Format(Now(),"MYY") & format(rs!MaxCaseNo,"000")+1
  11. end if
  12.  
  13. rs.close
  14. set rs=nothing
  15.  
Apr 11 '07 #4
jl2886
83
What is a record set? AND should I just put that code after doubleclick of LSI CASE NUmber in form.
By the way I am importing the table from Excel, so some already have case numbers...will they become record set's?

Also, It's not working...it says that there is an operator missing. and it says that set is missing
Apr 11 '07 #5
NeoPa
32,556 Expert Mod 16PB
A Recordset is a set of records.
Simple really, but it can be a table or a select query. Select queries can be as simple as showing a single table or as complex as showing fields from many linked and/or unlinked tables or subqueries.
The rest of your post I don't understand as it is without reference. Which post are you responding to?
Apr 11 '07 #6
MMcCarthy
14,534 Expert Mod 8TB
What is a record set? AND should I just put that code after doubleclick of LSI CASE NUmber in form.
By the way I am importing the table from Excel, so some already have case numbers...will they become record set's?

Also, It's not working...it says that there is an operator missing. and it says that set is missing
Did you try my suggestion in post #2 and if so what was the result?
Apr 12 '07 #7
pks00
280 Expert 100+
Sorry there was a missing closing bracket in the code. Ive also corrected incrementing the next number

Now at what point would you assign a new ID? Is it at the point of adding a record?
If so then u need to make use of the form's current event. This is called each time a new record is loaded or added, then to check the newrecord flag
eg

Expand|Select|Wrap|Line Numbers
  1. public sub Form_Current()
  2.     Dim rs As DAO.Recordset
  3.     Dim sSql As String
  4.  
  5.     If Me.NewRecord = True Then
  6.         sSql = "SELECT MAX(  MID$([LSI Case Number], Instr(1,[LSI Case Number],'-')+1)) AS MaxCaseNo FROM [Master Log] WHERE Left$([LSI Case Number],Instr(1,[LSI Case Number],'-')-1) = " & Format(Now(), "MYY")
  7.         Debug.Print sSql
  8.  
  9.         Set rs = CurrentDb.OpenRecordset(sSql)
  10.         If rs.EOF = True Or IsNull(rs!MaxCaseNo) Then
  11.             Me.LSI_Case_Number = Format(Now(), "MYY") & "-001"
  12.         Else
  13.             Me.LSI_Case_Number = Format(Now(), "MYY") & "-" & Format(rs!MaxCaseNo + 1, "000")
  14.         End If
  15.  
  16.         rs.Close
  17.         Set rs = Nothing
  18.     End If
  19.  

With regards to a recordset, NeoPa has given u an explanation.
A little further explanation - Im not sure how else to describe it but I'll give it a go

Basically a recordset is a variable that holds all the fields that you selected by specifying the sql (or table) and its values and properties.
It is a way in code to read records and also add, update and delete records.
Apr 12 '07 #8
jl2886
83
Did you try my suggestion in post #2 and if so what was the result?
Hi, it reads invalid use of the null?
Apr 12 '07 #9
jl2886
83
Hi, it reads invalid use of the null?
Mccarthy's method reads invalid use of the null.
The recordset method, doesn't do anything? Do I need to click something, or just click the next arrows until I get to a new record
Apr 12 '07 #10
MMcCarthy
14,534 Expert Mod 8TB
What does this do ...

Expand|Select|Wrap|Line Numbers
  1. Private Sub LSI_Case_Number_DblClick(Cancel As Integer)
  2. Dim assID As Long
  3.    assID = nz(DMax("CLng(Right([LSI Case Number],3))", "Master Log", Left([LSI Case Number], InStr([LSI Case Number], "-")-1) = CStr(Month(Now())) & CStr(Year(Now()))), "001")
  4.    Me.LSI_Case_Number = CStr(Month(Now())) & CStr(Year(Now())) & "-" & Format(assID + 1, "000")
  5. End Sub
  6.  
Mary
Apr 12 '07 #11
jl2886
83
It reads invalid use of null. Then when I open the VBA Code, it says indentifier under cursor is not recognized


What does this do ...

Expand|Select|Wrap|Line Numbers
  1. Private Sub LSI_Case_Number_DblClick(Cancel As Integer)
  2. Dim assID As Long
  3.    assID = nz(DMax("CLng(Right([LSI Case Number],3))", "Master Log", Left([LSI Case Number], InStr([LSI Case Number], "-")-1) = CStr(Month(Now())) & CStr(Year(Now()))), "001")
  4.    Me.LSI_Case_Number = CStr(Month(Now())) & CStr(Year(Now())) & "-" & Format(assID + 1, "000")
  5. End Sub
  6.  
Mary
Apr 17 '07 #12
MMcCarthy
14,534 Expert Mod 8TB
I've changed it very slightly, can you run it again and if it still gives an error can you tell me exactly which line of code it stops on.

Expand|Select|Wrap|Line Numbers
  1. Private Sub LSI_Case_Number_DblClick(Cancel As Integer)
  2. Dim assID As Long
  3.  
  4.    assID = DMax("CLng(Right([LSI Case Number],3))", "Master Log", Left([LSI Case Number], InStr([LSI Case Number], "-")-1) = CStr(Month(Now())) & CStr(Year(Now())))
  5.    assID = assID+1
  6.    Me.LSI_Case_Number = CStr(Month(Now())) & CStr(Year(Now())) & "-" & Format(assID, "000")
  7. End Sub
  8.  
Mary
Apr 17 '07 #13
jl2886
83
Expand|Select|Wrap|Line Numbers
  1. Private Sub LSI_Case_Number_DblClick(Cancel As Integer)
  2. Dim assID As Long
  3.  
  4.    assID = DMax("CLng(Right([LSI Case Number],3))", "Master Log", Left([LSI Case Number], InStr([LSI Case Number], "-") - 1) = CStr(month(Now())) & CStr(Year(Now())))
  5.  
  6.    assID = assID + 1
  7.    Me.LSI_Case_Number = CStr(month(Now())) & CStr(Year(Now())) & "-" & Format(assID, "000")
  8. End Sub
  9.  
The bolded line reads identifier under cursor not recognized. LSI Case Number is Text


I've changed it very slightly, can you run it again and if it still gives an error can you tell me exactly which line of code it stops on.

Expand|Select|Wrap|Line Numbers
  1. Private Sub LSI_Case_Number_DblClick(Cancel As Integer)
  2. Dim assID As Long
  3.  
  4.    assID = DMax("CLng(Right([LSI Case Number],3))", "Master Log", Left([LSI Case Number], InStr([LSI Case Number], "-")-1) = CStr(Month(Now())) & CStr(Year(Now())))
  5.    assID = assID+1
  6.    Me.LSI_Case_Number = CStr(Month(Now())) & CStr(Year(Now())) & "-" & Format(assID, "000")
  7. End Sub
  8.  
Mary
Apr 18 '07 #14
pks00
280 Expert 100+
Hi, how did u run the code?
I set it in form_current and it only gets run when u go into addmode

here is that same example but with diagnostics

Expand|Select|Wrap|Line Numbers
  1. Public Sub Form_Current()
  2.     Dim rs As DAO.Recordset
  3.     Dim sSql As String
  4.  
  5.     MsgBox "In Form Current"
  6.     If Me.NewRecord = True Then
  7.  
  8.         MsgBox "In Add Mode"
  9.  
  10.         sSql = "SELECT MAX(  MID$([LSI Case Number], Instr(1,[LSI Case Number],'-')+1)) AS MaxCaseNo FROM [Master Log] WHERE Left$([LSI Case Number],Instr(1,[LSI Case Number],'-')-1) = " & Format(Now(), "MYY")
  11.  
  12.         MsgBox "SQL Generated - please see immediate window, take sql generated and run in sql query editor"
  13.         Debug.Print sSql
  14.  
  15.         Set rs = CurrentDb.OpenRecordset(sSql)
  16.         If rs.EOF = True Or IsNull(rs!MaxCaseNo) Then
  17.             Me.LSI_Case_Number = Format(Now(), "MYY") & "-001"
  18.             MsgBox "EOF encounterd - LSI set to " & Me.LSI_Case_Number
  19.         Else
  20.             Me.LSI_Case_Number = Format(Now(), "MYY") & "-" & Format(rs!MaxCaseNo + 1, "000")
  21.             MsgBox "Record Found - LSI set to " & Me.LSI_Case_Number
  22.         End If
  23.  
  24.         rs.Close
  25.         Set rs = Nothing
  26.     End If
  27. End Sub
  28.  
Apr 18 '07 #15
jl2886
83
the diagnostic boxes come up when I click the arrow and asterik in the form. Then it reads an error datatype mismatch in criteria expression and then it highlights the part in the code that reads " Set rs = CurrentDb.OpenRecordset(sSql)" and the defninition reads indentifier cursor not recognized

Hi, how did u run the code?
I set it in form_current and it only gets run when u go into addmode

here is that same example but with diagnostics

Expand|Select|Wrap|Line Numbers
  1. Public Sub Form_Current()
  2.     Dim rs As DAO.Recordset
  3.     Dim sSql As String
  4.  
  5.     MsgBox "In Form Current"
  6.     If Me.NewRecord = True Then
  7.  
  8.         MsgBox "In Add Mode"
  9.  
  10.         sSql = "SELECT MAX(  MID$([LSI Case Number], Instr(1,[LSI Case Number],'-')+1)) AS MaxCaseNo FROM [Master Log] WHERE Left$([LSI Case Number],Instr(1,[LSI Case Number],'-')-1) = " & Format(Now(), "MYY")
  11.  
  12.         MsgBox "SQL Generated - please see immediate window, take sql generated and run in sql query editor"
  13.         Debug.Print sSql
  14.  
  15.         Set rs = CurrentDb.OpenRecordset(sSql)
  16.         If rs.EOF = True Or IsNull(rs!MaxCaseNo) Then
  17.             Me.LSI_Case_Number = Format(Now(), "MYY") & "-001"
  18.             MsgBox "EOF encounterd - LSI set to " & Me.LSI_Case_Number
  19.         Else
  20.             Me.LSI_Case_Number = Format(Now(), "MYY") & "-" & Format(rs!MaxCaseNo + 1, "000")
  21.             MsgBox "Record Found - LSI set to " & Me.LSI_Case_Number
  22.         End If
  23.  
  24.         rs.Close
  25.         Set rs = Nothing
  26.     End If
  27. End Sub
  28.  
Apr 18 '07 #16
jl2886
83
by the way, I am inputting the code right into the visual basic editor within the form. I am not creating a module.
Apr 18 '07 #17
pks00
280 Expert 100+
thats fine, ensure the event is registered
looking at form's properties, go to Events tab and click on On Current
then code is called
next id is calculated when u create a new record
Apr 18 '07 #18
jl2886
83
I don't know what I am doing, but it won't work. Did you test it out?
First. I open the form. Then I go to design view. then i open the vba editor.
Then I copy and paste your code into the editor. Then I close the editor and save. Then I save teh form. Then I click on next record.
When I do that, two boxes come up. then the debugger comes up with datatype mismatched. I click debugged and the line with "Set rs = CurrentDb.OpenRecordset(sSql)"
Apr 18 '07 #19
pks00
280 Expert 100+
look in the immediate window (from vba window)
there should be some SQL generated
can u please take that and run it

go to query, new query, cancel if it prompts u for a table, then go to sql view and paste it then run it

also paste the sql generated here
Apr 18 '07 #20
jl2886
83
the Sql generated
SELECT MAX( MID$([LSI Case Number], Instr(1,[LSI Case Number],'-')+1)) AS MaxCaseNo FROM [Master Log] WHERE Left$([LSI Case Number],Instr(1,[LSI Case Number],'-')-1) = 407

but then when i go up to run and continue the set rs is highlighted
look in the immediate window (from vba window)
there should be some SQL generated
can u please take that and run it

go to query, new query, cancel if it prompts u for a table, then go to sql view and paste it then run it

also paste the sql generated here
Apr 18 '07 #21
jl2886
83
when i opened a new query and ran the sqly code it read data mismatch error


the Sql generated
SELECT MAX( MID$([LSI Case Number], Instr(1,[LSI Case Number],'-')+1)) AS MaxCaseNo FROM [Master Log] WHERE Left$([LSI Case Number],Instr(1,[LSI Case Number],'-')-1) = 407

but then when i go up to run and continue the set rs is highlighted
Apr 18 '07 #22
pks00
280 Expert 100+
What are the values of LSI Case Number in the database

are they all the same format?
do u have any nulls? any in other formats?
Apr 18 '07 #23
jl2886
83
YES I DO, i just realized. Maybe I should remove them.

What are the values of LSI Case Number in the database

are they all the same format?
do u have any nulls? any in other formats?
Apr 19 '07 #24
NeoPa
32,556 Expert Mod 16PB
YES I DO, i just realized. Maybe I should remove them.
Remove them - or handle them in your code.
Whichever is the appropriate course of action in your circumstances.
Apr 19 '07 #25

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

Similar topics

2
by: Nick | last post by:
Loop to create an array from a dynamic form. I'm having trouble with an application, and I'll try to explain it as clearly as possible: 1. I have a form with two fields, say Apples and...
4
by: Melissa | last post by:
I have a frontend file named CustomerApp and backend file named CustomerData. CustomerApp is at C:\Customer Database and CustomerData is at S:\Customer Database. Could someone help me with the code...
2
by: Alicia | last post by:
Does anyone know why I am getting a "Syntax error in Create Table statement". I am using Microsoft Access SQL View to enter it. Any other problems I may run into? CREATE TABLE weeks (...
3
by: blindsey | last post by:
Is there a tool that can take an Access database and generate SQL "CREATE TABLE" statements for all the tables in it?
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
27
by: max | last post by:
Hello, I am a newbye, and I'm trying to write a simple application. I have five tables with three columns; all tables are identical; I need to change some data in the first table and let VB...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
0
by: ramuygl | last post by:
want to create store procedure that. want to send the table name as argument and retrive the data of that argument. and want to store data in temperary table using the insert query. HERE I AM...
1
by: TG | last post by:
Hi! I have an application in which I have some checkboxes and depending which ones are checked those columns will show in the datagridview from sql server or no. After that I have 2 buttons:...
3
by: DeanL | last post by:
Hi guys, Does anyone know of a way to create multiple tables using information stored in one table? I have a table with 4 columns (TableName, ColumnName, DataType, DataSize) and wanted to know...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.