473,406 Members | 2,705 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,406 software developers and data experts.

2 related access tables connected to one vb form, problem with saving data

3
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.
Attached Images
File Type: jpg untitled.jpg (18.1 KB, 511 views)
Jun 28 '10 #1

✓ answered by jimatqsi

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

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public Function NextID(strTable, Optional strPrefix, Optional bVerify As Boolean) As String
  5. Dim dbs As DAO.Database
  6. Dim rs As DAO.Recordset
  7. Dim lngNextID As Long
  8. Dim rsTargetTbl As DAO.Recordset    '
  9. Dim strSQL As String
  10.  
  11. ' strTable is the table we want to form a new key for
  12. ' strPrefix is an optional prefix for the formulated key
  13. ' bVerify indicates whether to verify the key is unique or not
  14.  
  15. On Error GoTo Err_NextID
  16.  
  17. Set dbs = CodeDb
  18. NextID = ""
  19. Do Until NextID <> ""
  20.     ' select only the row containing the ID for the selected table
  21.     strSQL = "Select * from tblTableIDs where strTableName=""" & strTable & """"
  22.     Set rs = dbs.OpenRecordset("tblTableIDs", dbOpenDynaset, dbSeeChanges)
  23.         rs.Edit
  24.             lngNextID = rs!NextID + 1
  25.             rs!NextID = lngNextID
  26.         rs.Update
  27.  
  28.         If Nz(strPrefix, "") = "" Then ' was a prefix supplied?
  29.             NextID = Format(lngNextID, "000000") ' you might want a different format
  30.         Else
  31.             NextID = strPrefix & Format(lngNextID, "000000") ' add the prefix to the key
  32.         End Function
  33.     rs.Close
  34.  
  35.     If Nz(bVerify, False) = True Then
  36.         strSQL = "select ID from " & strTable & " where ID=""" & NextID & """"
  37.         Set rsTargetTbl = dbs.OpenRecordset(strSQL, dbReadOnly)
  38.         If Not rstargetbl.EOF Then
  39.             NextID = "" ' can't use this ID because it is already in the table
  40.         End If
  41.         rsTargetTbl.Close
  42.     End If
  43. Loop        ' until nextid is not ""
  44.  
  45. Exit_NextID:
  46.     rs.Close
  47.     rsTargetTbl.Close
  48.     Set rs = Nothing
  49.     Set rsTargetTbl = Nothing
  50.     Set dbs = Nothing
  51. Exit Function
  52.  
  53. Err_NextID:
  54.     NextID = ""
  55.     GoTo Exit_NextID
  56.  
  57.  

5 3735
jimatqsi
1,271 Expert 1GB
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
Jun 29 '10 #2
AiVrea
3
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.
Jun 29 '10 #3
jimatqsi
1,271 Expert 1GB
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
Jun 29 '10 #4
jimatqsi
1,271 Expert 1GB
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

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public Function NextID(strTable, Optional strPrefix, Optional bVerify As Boolean) As String
  5. Dim dbs As DAO.Database
  6. Dim rs As DAO.Recordset
  7. Dim lngNextID As Long
  8. Dim rsTargetTbl As DAO.Recordset    '
  9. Dim strSQL As String
  10.  
  11. ' strTable is the table we want to form a new key for
  12. ' strPrefix is an optional prefix for the formulated key
  13. ' bVerify indicates whether to verify the key is unique or not
  14.  
  15. On Error GoTo Err_NextID
  16.  
  17. Set dbs = CodeDb
  18. NextID = ""
  19. Do Until NextID <> ""
  20.     ' select only the row containing the ID for the selected table
  21.     strSQL = "Select * from tblTableIDs where strTableName=""" & strTable & """"
  22.     Set rs = dbs.OpenRecordset("tblTableIDs", dbOpenDynaset, dbSeeChanges)
  23.         rs.Edit
  24.             lngNextID = rs!NextID + 1
  25.             rs!NextID = lngNextID
  26.         rs.Update
  27.  
  28.         If Nz(strPrefix, "") = "" Then ' was a prefix supplied?
  29.             NextID = Format(lngNextID, "000000") ' you might want a different format
  30.         Else
  31.             NextID = strPrefix & Format(lngNextID, "000000") ' add the prefix to the key
  32.         End Function
  33.     rs.Close
  34.  
  35.     If Nz(bVerify, False) = True Then
  36.         strSQL = "select ID from " & strTable & " where ID=""" & NextID & """"
  37.         Set rsTargetTbl = dbs.OpenRecordset(strSQL, dbReadOnly)
  38.         If Not rstargetbl.EOF Then
  39.             NextID = "" ' can't use this ID because it is already in the table
  40.         End If
  41.         rsTargetTbl.Close
  42.     End If
  43. Loop        ' until nextid is not ""
  44.  
  45. Exit_NextID:
  46.     rs.Close
  47.     rsTargetTbl.Close
  48.     Set rs = Nothing
  49.     Set rsTargetTbl = Nothing
  50.     Set dbs = Nothing
  51. Exit Function
  52.  
  53. Err_NextID:
  54.     NextID = ""
  55.     GoTo Exit_NextID
  56.  
  57.  
Jun 29 '10 #5
AiVrea
3
Thanks for the code, it's perfect for what I needed.
Jul 1 '10 #6

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

Similar topics

3
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....
0
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...
2
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...
6
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...
1
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...
0
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...
5
imrosie
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...
4
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...
0
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
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...
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
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...
0
Oralloy
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,...
0
jinu1996
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...
0
isladogs
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...

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.