Hi, I have 2 access tables (table1, table2), table2 is related to the field "Nr Crt"(which is an AutoNumber) in table1 in a "one-to-many" relation. Table1 is connected to the vb form to textboxes, table2 is in DataGridView. Whenever I try to add data, the AutoNumber is set to -1, so the data from table2 is not saved correctly.I need a code for when I click the Add button the "Nr Crt" autonumber field will automatically be increased by 1.
The circled textbox in the image is the "Nr Crt" field in table1, and the circled field in the DataGridView is the related field from table2.The picture is taken right after I pressed the Add button.And I need a code that will automatically increase the "Nr Crt" field by 1 instead of setting it to -1.
Here's the code for generating your own keys. It makes some assumptions about the names of tables and columns and key format, but you can alter that to suit your DB scheme.
Jim - Option Compare Database
-
Option Explicit
-
-
Public Function NextID(strTable, Optional strPrefix, Optional bVerify As Boolean) As String
-
Dim dbs As DAO.Database
-
Dim rs As DAO.Recordset
-
Dim lngNextID As Long
-
Dim rsTargetTbl As DAO.Recordset '
-
Dim strSQL As String
-
-
' strTable is the table we want to form a new key for
-
' strPrefix is an optional prefix for the formulated key
-
' bVerify indicates whether to verify the key is unique or not
-
-
On Error GoTo Err_NextID
-
-
Set dbs = CodeDb
-
NextID = ""
-
Do Until NextID <> ""
-
' select only the row containing the ID for the selected table
-
strSQL = "Select * from tblTableIDs where strTableName=""" & strTable & """"
-
Set rs = dbs.OpenRecordset("tblTableIDs", dbOpenDynaset, dbSeeChanges)
-
rs.Edit
-
lngNextID = rs!NextID + 1
-
rs!NextID = lngNextID
-
rs.Update
-
-
If Nz(strPrefix, "") = "" Then ' was a prefix supplied?
-
NextID = Format(lngNextID, "000000") ' you might want a different format
-
Else
-
NextID = strPrefix & Format(lngNextID, "000000") ' add the prefix to the key
-
End Function
-
rs.Close
-
-
If Nz(bVerify, False) = True Then
-
strSQL = "select ID from " & strTable & " where ID=""" & NextID & """"
-
Set rsTargetTbl = dbs.OpenRecordset(strSQL, dbReadOnly)
-
If Not rstargetbl.EOF Then
-
NextID = "" ' can't use this ID because it is already in the table
-
End If
-
rsTargetTbl.Close
-
End If
-
Loop ' until nextid is not ""
-
-
Exit_NextID:
-
rs.Close
-
rsTargetTbl.Close
-
Set rs = Nothing
-
Set rsTargetTbl = Nothing
-
Set dbs = Nothing
-
Exit Function
-
-
Err_NextID:
-
NextID = ""
-
GoTo Exit_NextID
-
-
5 3735
Perhaps you've heard this before, but keying or linking tables based on an auto-number field is a very, very bad idea. Those creatures can get corrupted and have to be reset, plus you have problems like the one you're grappling with now.
You're going to have to base this form on a query to one table only. When an add gets done, it will add only a record to that table. Then you'll have to add some code fired by an event handler to add the data to table 2 based on what just got added to table 1.
You would be much better off to draw a line in the sand right now to say "no more keys or joins on auto-number fields" and do the necessary coding to make a function to maintain a "next available number" parameter table. I'll post some code I've got that does that very thing.
Jim
Yeah I pretty much understood the problem and I was thinking to add 2 extra buttons of add and save for the secondary table. So when I click the add button on top it only connects to table1 and the save button on top also saves only in table1. And the 2 new buttons will add and save in table2. I'll leave the NrCrt(table1) textbox and the corresponding field in table2 visible so that after saving data into table1 the Nr Crt will be visible and can be entered in table2 in the DataGrid view.
Tell me if you think it's a good idea. and thx for the reply.
No, it does not sound like a very good design plan. You want to make one form independently maintaining two unjoined tables. Requires too much attention on the part of the user and you're very likely to end up with unmatched records in each table. There's no good reason for it. It's not like you're up against a wall, with no other good strategies available.
Add a column to each table to represent a real key field. Run an update query that will copy the auto-generated number into the new key field in each table. Then change the form to generate keys for any future records added to table one, and change the underlying query to join based on this new key field rather than the auto-gen field.
Jim
Here's the code for generating your own keys. It makes some assumptions about the names of tables and columns and key format, but you can alter that to suit your DB scheme.
Jim - Option Compare Database
-
Option Explicit
-
-
Public Function NextID(strTable, Optional strPrefix, Optional bVerify As Boolean) As String
-
Dim dbs As DAO.Database
-
Dim rs As DAO.Recordset
-
Dim lngNextID As Long
-
Dim rsTargetTbl As DAO.Recordset '
-
Dim strSQL As String
-
-
' strTable is the table we want to form a new key for
-
' strPrefix is an optional prefix for the formulated key
-
' bVerify indicates whether to verify the key is unique or not
-
-
On Error GoTo Err_NextID
-
-
Set dbs = CodeDb
-
NextID = ""
-
Do Until NextID <> ""
-
' select only the row containing the ID for the selected table
-
strSQL = "Select * from tblTableIDs where strTableName=""" & strTable & """"
-
Set rs = dbs.OpenRecordset("tblTableIDs", dbOpenDynaset, dbSeeChanges)
-
rs.Edit
-
lngNextID = rs!NextID + 1
-
rs!NextID = lngNextID
-
rs.Update
-
-
If Nz(strPrefix, "") = "" Then ' was a prefix supplied?
-
NextID = Format(lngNextID, "000000") ' you might want a different format
-
Else
-
NextID = strPrefix & Format(lngNextID, "000000") ' add the prefix to the key
-
End Function
-
rs.Close
-
-
If Nz(bVerify, False) = True Then
-
strSQL = "select ID from " & strTable & " where ID=""" & NextID & """"
-
Set rsTargetTbl = dbs.OpenRecordset(strSQL, dbReadOnly)
-
If Not rstargetbl.EOF Then
-
NextID = "" ' can't use this ID because it is already in the table
-
End If
-
rsTargetTbl.Close
-
End If
-
Loop ' until nextid is not ""
-
-
Exit_NextID:
-
rs.Close
-
rsTargetTbl.Close
-
Set rs = Nothing
-
Set rsTargetTbl = Nothing
-
Set dbs = Nothing
-
Exit Function
-
-
Err_NextID:
-
NextID = ""
-
GoTo Exit_NextID
-
-
Thanks for the code, it's perfect for what I needed.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jim Adams |
last post by:
My ASP.Net (VB.Net) app needs to display a filled in form to a web
user from the following:
a) read a form files dynamically from disk (e.g. form1.html)
b) read its corresponding XML file (e.g....
|
by: Jeff D. Hamann |
last post by:
Sorry for the seemingly novice posting, but I could find a solution for this
on the web so far...
I've been developing a database using postgresql (and loving it) and have
started running into...
|
by: laks |
last post by:
Hi
I'm opening an aspx page with javascript:
window.showModalDialog("mypage.aspx", "", "status:no");
In mypage.aspx I've got a button which shall first save the displayed
record
in the C# code...
|
by: erick-flores |
last post by:
Hello all,
Form A & Form B
Form B opens when I click on a button from Form A. How do I setup Form
B so it will always let me insert new records. Because right know, when
I click on the button...
|
by: captain_gni |
last post by:
Hi All, New to asp.net here. Been combing google since monday trying to
find the right solution. Most of the solutions are good and reflect the
same examples in the .net books I have but xml is...
|
by: =?Utf-8?B?QkFUT04=?= |
last post by:
I have realy big problem I try lot of things but never success!
I need a HELP!
Problem is in that I made database in Microsoft access and connect her to C#
form and datagridview. and it...
|
by: imrosie |
last post by:
Hello any expert out there. I'm yet another newbie (yan) who needs lots of help. Here's my issue. I'm trying to create an product ordering database.
I set up a 'Search' form with the ability to...
|
by: christopher.catlin |
last post by:
Hi,
I'm currently designing a database for a construction co and learning
how to use access on the fly. I'm having a few problems with the re-
use of data if I link tables using relationships...
|
by: Kassimu |
last post by:
Hi guys out there,
There is this database Iam creating, I have a table with 40 fields among which there are Date/time, Text, Number, Memo and Yes/No fields and I have created the form bound to that...
|
by: JamieC |
last post by:
Hi, I have a form for customers to enter requests, this form posts to a php script which checks if all fields have been populated and, if so, sends the data to a database. If not all fields have been...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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...
|
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,...
|
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: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| | |