Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old October 11th, 2009, 09:38 PM
Daniel Shiel Daniel Shiel is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Posts: 19 Daniel Shiel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 58 m 56 sec
Reputation Power: 0
AutoNumber to generate only if field has value

I'm not really sure if this is possible but would appreciate any feedback or thoughts.

I have a field fld_FeedbackNumber which is set as an Autonumber.

The problem is not the autonumber itself, but when a user opens the database to input a new record and doesn't enter any data then exits.

This effectively creates a blank record.

To try and combat this i created a subform that opens first and has 4 buttons, Log New, Update Existing, Reports & Cancel/Exit.

For some reason people still get it wrong and press Log New when they do not want to Log New which generates a new blank record after the last.

I have a couple of other ideas on how to avoid this but am not sure how to execute them.

A: Set the autonumber field to not generate the number until the last required field has been updated & exited, or the submit/exit button has been pressed (which is another button i have on my main form).

B: OnOpen - If user presses Log New and fld_ClaimNumber (the set focus field when Log New is pressed) = "" then goto that record instead of creating a new record. This is probably not the best idea though because if there is more than 1 blank record it would presumably go to the last blank record.

C: Have a dialogue box that comes up and says "If you have pressed Log New and do not intend to log a new feedback then you have just created a blank record which will forever remain blank unless the administrator goes and rectifies your mistake, thus wasting their valuable time - thank you".

If there is a better suggestion out there i would be glad to hear it as it's quite frustrating having to go and remove blank records then cut paste the table into another database, then cut and paste back into original database just to keep the autonumber incremental without gaps.

Reply With Quote
  #2  
Old October 11th, 2009, 10:51 PM
June7 June7 is offline
Contributing User
Click here for more information.
 
Join Date: Apr 2009
Location: The Great Land
Posts: 536 June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 13 h 58 m 7 sec
Reputation Power: 124
Item A: You cannot set the AutoNumber field to not generate. My experience is that once you enter a field on that row, the number is committed.

Item B: You could have code that finds the first blank record, would have to use a field that would always have data for a completed record.

Item C: Better to find a solution that handles the idiots, never perfect, but just have to handle each situation as they arise.

Another option is to not care what the autonumber value is and utilize a structured unique identifier, which is actually better database design. My project uses code to create a unique ID for each record (these are laboratory sample ID's, structured like yyyyA-9999). I do have one table with an AutoNumber field that is used to identify a unique project and is a foreign key for the sample table and is used nowhere else. It is a pain to deal with but I tolerate it because otherwise I would have a compound unique key composed of 4 fields. This one table is the only table that even has an autonumber field. Pretty much everything else is linked by the sample ID.

Last edited by June7 : October 11th, 2009 at 10:54 PM.

Reply With Quote
  #3  
Old October 11th, 2009, 11:15 PM
Daniel Shiel Daniel Shiel is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Posts: 19 Daniel Shiel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 58 m 56 sec
Reputation Power: 0
Hi June7, thanks for the response.

Looks like Option B is the answer.

As for the idiots, i found that no matter how idiot proof you make something they create a better idot.

Can you give me some assistance on how i would go about the code for finding the first blank record and setting it look for it etc. The If criteria would have to be if fld_ClaimNumber = "" Then Search or find etc etc.

I had never used access before i started this database so it's been one massive learning experience.

Reply With Quote
  #4  
Old October 12th, 2009, 02:09 PM
June7 June7 is offline
Contributing User
Click here for more information.
 
Join Date: Apr 2009
Location: The Great Land
Posts: 536 June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 13 h 58 m 7 sec
Reputation Power: 124
Code from my project. Looks for record with sample ID but no other data because of aborted login. Once sample ID is established, opens data entry form to existing or new record.
Code:
Private Sub btnLoginSample_Click()

Dim rs As ADODB.Recordset
Dim cn As ADODB.Connection
Dim strLabNum As String

Set cn = CurrentProject.Connection
Set rs = New ADODB.Recordset

'search for aborted lab number and use it, else if none then GetNewLabNumber
rs.Open "SELECT LabNum FROM Submit WHERE IsNull(DateEnter);", cn, adOpenStatic, adLockPessimistic

If rs.RecordCount > 0 Then
    rs.MoveFirst
    'strLabNum is retrieved number from an aborted login
    cn.Execute "UPDATE Submit SET DateEnter=#" & Date & "#, EnterWho='" & gstrUser & "' WHERE LabNum='" & rs!LABNUM & "'"
    strLabNum = rs!LABNUM
Else
    strLabNum = GetNewLabNumber
    cn.Execute "INSERT INTO Submit (LabNum, DateEnter, EnterWho) VALUES ('" & strLabNum & "', #" & Date & "#, '" & gstrUser & "')"
End If

rs.Close

Me.tbxLABNUM.SetFocus

DoCmd.OpenForm "Sample", acNormal, , "LabNum='" & strLabNum & "'", , , strLabNum & "," & "Login"

End Sub
This function was designed to initialize the very first sample ID in a new blank database, subsequent logins checks if in new year and need to restart the sequence, else continue with current sequence.
Code:
Public Function GetNewLabNumber() As String

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strLabNum As String

Set cn = CurrentProject.Connection
Set rs = New ADODB.Recordset

rs.Open "SELECT LabNum FROM Submit ORDER BY LabNum DESC", cn, adOpenForwardOnly, adLockPessimistic

If rs.EOF = False Then
    strLabNum = rs.Fields(0)
    If CDate(Left(strLabNum, 4)) = DatePart("yyyy", Date) Then
        strLabNum = Left(strLabNum, 6) & Format(Right(strLabNum, 4) + 1, "0000")
    Else
        strLabNum = DatePart("yyyy", Date) & "A-0001"
    End If
Else
    strLabNum = DatePart("yyyy", Date) & "A-0001"
End If

rs.Close

GetNewLabNumber = strLabNum

End Function
Your solution may be much simpler. Possible you could get by with a DLookup on the table which would not require establishing data connection as I do. Ex.
Code:
strID = Nz(DLookup("IDfieldname", "tableName", "Isnull(criteriafieldname)"), 0)
If strID = 0 Then
     'code to open form to new record
Else
     'code to open form to existing record
End If
'I use Isnull because I don't allow empty strings in my tables.
'If you do then modify to something like:
strID = DLookup("IDfieldname","tableName", "criteriafieldname & ''=''")
If strID = "" Then
...
'Am not sure about the empty string handling as I have never needed this in a DLookup Where clause
Make code attempt and post for analysis.

Last edited by June7 : October 12th, 2009 at 02:36 PM.

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingVisual Basic Programming > AutoNumber to generate only if field has value


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump





 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek