473,466 Members | 3,167 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Why is primary key value null?

beldom
10 New Member
I'm having the error message "Index or primary key cannot contain null value" pop up even though I haven't updated or otherwise changed the primary key. Why could this be?
Jul 21 '14 #1
16 1709
Seth Schrock
2,965 Recognized Expert Specialist
Are you working with related tables at the same time? Do you have any code or macros involved that would be modifying the data?
Jul 22 '14 #2
beldom
10 New Member
Nope. The only thing the form does is search (select) items from the table. Tomorrow I will try two things: 1) Delete the table and start a new one to make sure that there are no settings or other criteria that make an empty cell in the primary key field 2) Delete the code in the button and close the form, because that is when the error message appears. Will let you know then if either solution works.
Jul 22 '14 #3
twinnyfo
3,653 Recognized Expert Moderator Specialist
If your primary key is not an autonumber, you may be moving to a new record? If you post the settings of your table and the code in your Form, perhaps we could troubleshoot better.
Jul 22 '14 #4
beldom
10 New Member
I don't know what you mean by settings of my table.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSearch_Click()
  2.     On Error Resume Next
  3.     Dim ctl As Control
  4.     Dim sSQL As String
  5.     Dim sWhereClause As String
  6. Dim db As DAO.Database
  7. Dim rs As DAO.Recordset
  8. 'Dim strSQL As String
  9. Set db = CurrentDb
  10.     'Initialize the Where Clause variable.
  11.     sWhereClause = " Where "
  12.     'Start the first part of the select statement.
  13.     sSQL = "select * from Patients "
  14.     'sSQL = "INSERT INTO Patients (MedRec,Gender) VALUES (1121, 'R');"
  15.  
  16.     'Loop through each control on the form to get its value.
  17.     For Each ctl In Me.Controls
  18.         With ctl
  19.             'The only Control you are using is the text box. However, you can add as many types of controls as you want.
  20.             Select Case .ControlType
  21.                 Case acTextBox
  22.                     .SetFocus
  23.                     'This is the function that actually builds the clause.
  24.                     If sWhereClause = " Where " Then
  25.                         sWhereClause = sWhereClause & BuildCriteria(.Name, dbText, .Text)
  26.                     Else
  27.                         sWhereClause = sWhereClause & " and " & BuildCriteria(.Name, dbText, .Text)
  28.                     End If
  29.             End Select
  30.         End With
  31.     Next ctl
  32.     'Set the forms recordsource equal to the new select statement.
  33.     Me.txtSQL = sSQL & sWhereClause & ";"
  34.     'Me.RecordSource = sSQL & sWhereClause & ";"    'Me.Query = sSQL & sWhereClause   'Me.Requery
  35.     'db.Execute sSQL & sWhereClause & ";", dbFailOnError
  36.  
  37. 'Me.List149.RowSource = db.OpenRecordset("select * from Patients;")
  38. Set rs = db.OpenRecordset(sSQL & sWhereClause & ";")
  39.  
  40.     'Do While Not rs.EOF
  41.     '    Me.List149.AddItem (rs![First Name] & ";" & rs![Last Name])
  42.     '    rs.MoveNext
  43.     'Loop
  44.  
  45.                 'Dim db As DAO.Database
  46.                 'Dim rs As DAO.Recordset
  47.                 Dim lst As ListBox
  48.                 'strBuild = ""
  49.  
  50.                 Set lst = Me![List149]
  51.  
  52.                 'Set up the Listbox correctly, if it isn't already
  53.                 lst.RowSourceType = "Value List"
  54.                 lst.ColumnCount = 3     '##################################################################
  55.                 lst.BoundColumn = 3     '##################################################################
  56.                 lst.ColumnWidths = "1 in;1 in;1 in" '##################################################################
  57.  
  58.                 'Set db = CurrentDb
  59.  
  60.                 'Set rs = db.OpenRecordset("SELECT * FROM AnswerT WHERE QuestionID =" & QuestionID)
  61.  
  62.                 Do While Not rs.EOF
  63.                     '##################################################################
  64.                     strBuild = strBuild & rs!Gender & ";" & rs![Last Name] & ";" & rs![First Name] & ";"
  65.                      rs.MoveNext
  66.                 Loop
  67.  
  68.                 lst.RowSource = Left$(strBuild, Len(strBuild) - 1)
  69.  
  70.  
  71.  
  72.  
  73.  
  74. rs.Close
  75. Set rs = Nothing
  76.     Set db = Nothing
  77.     Me.Requery
  78.     Me.Refresh
  79.     End Sub
  80.  
  81.  
Jul 22 '14 #5
twinnyfo
3,653 Recognized Expert Moderator Specialist
Is your primary key an autonumber, or do you set the value in another way?

And, which line of the code you posted is generating the error?
Jul 22 '14 #6
beldom
10 New Member
Anyway, I created a new table (called "Patients") and it seems to have fixed the issue.
Anyone know how I can make it so when I input a blank search value the result shows a blank listbox instead of causing Access to freeze and have to be restarted?
Jul 22 '14 #7
beldom
10 New Member
It wasn't the code, it was the table. It wasn't an autonumber but it is now, which seems to have been what was wrong...Perhaps when inputting data into the table I forgot to input a primary key (which was the Medical Record # for patients) and even though I later appended the missing field, Access held a grudge. So it would seem.
Jul 22 '14 #8
twinnyfo
3,653 Recognized Expert Moderator Specialist
Concerning Post #7, to prevent errors, when you use a blank control, you can use a condition:

Expand|Select|Wrap|Line Numbers
  1. If Not IsNull(Me.ControlName) Then...
That should prevent certain problems...
Jul 22 '14 #9
beldom
10 New Member
Expand|Select|Wrap|Line Numbers
  1. If Not IsNull(Me.Controls) Then
  2.                     strBuild = strBuild & "" & ";" & "" & ";" & "" & ";"
  3.                      rs.MoveNext
  4.                      End If
This clears the listbox, but also, this kills the database (crashes). What's wrong here?
Jul 22 '14 #10
twinnyfo
3,653 Recognized Expert Moderator Specialist
Insert a break point to determine at which line the DB is crashing. Sounds like something more than building a search string....
Jul 22 '14 #11
beldom
10 New Member
Breakpoint doesn't allow code to run, it just highlights the next line when I press F8...What do you mean, 'something more'? I'm just creating an alternative to the expected user input in case they press "Search" by accident when the field is still blank, so the DB doesn't freeze up on them...
Jul 24 '14 #12
twinnyfo
3,653 Recognized Expert Moderator Specialist
What I mean by "something more" is that typically a db does not crash because you build a text string. Sounds like there is something more going on.

And I wanted you to go through the code line by line with F8 to determine at which point the db kills itself. Like just mentioned, it would be terribly strange if the code in post #10 was causing a crash.
Jul 25 '14 #13
beldom
10 New Member
Expand|Select|Wrap|Line Numbers
  1. If Not IsNull(Me.Gender) Then
  2.     MsgBox "The input field is blank."
  3.  
  4.                 Else
  5.     Do While Not rs.EOF
  6.                     rs.MoveFirst
  7.                     strBuild = strBuild & rs!Gender & ";" & rs![Last Name] & ";" & rs![First Name] & ";"
  8.                      rs.MoveNext
  9.                 Loop
  10. End If
This is making the msgbox appear despite there being a letter in the input field. What am I doing wrong?

I'm going to implement a combobox for this task (searching patients by gender) in the future but I want to figure out how the Null condition works before I do, for future use.

Please and thanks guys!
Jul 29 '14 #14
twinnyfo
3,653 Recognized Expert Moderator Specialist
Is Me.Gender supposed to be referring to the Text Box or the Field itself? Believe it or not there is quite a difference. If there is no value in the field and the text box is changed, the code may still be looking at the field itself, as the record has not been updated (but is only considered "dirty").

First, some advice: I always intentionally name my text boxes based on what they are "txtGender" in this case or "cboComboBoxName", etc. This makes sure that the VBA will be looking at the specific control you want and not hte record underneath the form (both of which are valid way to refer to things). This also helps, particularly on Reports, when you want to sum across fields, as sometimes, a report will get confused when you want a sum of [FieldName] and there is also a text box named [FieldName].

Second, if, when you want to refer to a field name that has just been updated in a text box, I always guarantee that the field has been updated in the system by forcing a refresh. (Me.Refresh). In your case, if there is a value in the text box named Gender, it will force a save of that value to the field in the table. Then, whether the code is referring to the form's text box or the field itself, these values shoudl be the same.

Some of the experts here may be able to explain this better and understand it better than I can, but this has been my experience, and these few tips have proven very consistent and valuable over time.
Jul 29 '14 #15
zmbd
5,501 Recognized Expert Moderator Expert
Post# 14
If Me.Gender="A" then
Expand|Select|Wrap|Line Numbers
  1. If Not IsNull(Me.Gender) Then 
  2.  
IsNull()= false
then Not() = True
Thus the Message Box at line 2 and not the expect execution at line 5

Please read:
Bytes VBA Insights: What is Null?
Jul 29 '14 #16
twinnyfo
3,653 Recognized Expert Moderator Specialist
Completely missed that "Not" in OP's code....
Jul 29 '14 #17

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

Similar topics

1
by: Ken | last post by:
I wrote a function to use in queries that takes a date and adds or subtracts a certain length time and then returns the new value. There are times when my function needs to return Null values. ...
1
by: J | last post by:
Situation - Using the table pubs.titleauthor in a strongly typed dataset, attached to a datagrid, using the datagrid I would like to set the royaltyper field = null (the database allows nulls in...
3
by: Fabio Negri Cicotti [MCP] | last post by:
Hi All. I'm trying to insert data into 2 tables (parent-child) using the ADO.NET's SetParentRow method. The parent table has an identity column as primary key. When I execute the code below I...
12
by: Aidan | last post by:
I have a form to create a new record for a training course. The form is based on one table that has 4 keys set to primary key. The first combo box on the form allows selection of the course POP...
7
by: Sala | last post by:
Hi experts! If system time 12:00 am my application variable value will be null.. I am trying to get null value of application but that time i'll make some actions to that page.. So pls tell me how...
0
by: diane | last post by:
Just trying to upsize using VFP 9 with SQL 2005 using VFP remote views. One in particular keeps coming up and saying Cannot insert the value NULL into column, when I really don't think I am...
2
by: artev | last post by:
if I insert a string null in a select, it change position; why? I insert value "" in 2nd 3th select; <style type="text/css"> td {border:2px solid pink;}; </style>
9
by: ramdil | last post by:
Hi All I have a list box in a form with three columns 1.ID 2.Name 3.Age In the same form i have three text boxes corresponding for the above columns. The form record source property is set to...
4
by: qwedster | last post by:
Howdy folks! I am using stored procedure to see if a value exists in database table and return 0 if exists or else -1, in the following SQL queries. However how to check if a value (that is...
1
by: Octo Siburian | last post by:
I have been collecting data from ms.access database into a class object '_Get and put in data from database Fingerprint(RAS) into CPresensiFingerprint Public Function GetdbFingerprint() As...
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
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.