8 Replies - 1544 Views - Last Post: 08 February 2012 - 12:46 PM Rate Topic: -----

#1 Jerrrum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 08-February 12

Vb.net searching a database to see if value is already present

Posted 08 February 2012 - 09:06 AM

Hello, I am in the process of creating a program that is linked to a database. I want to automatically generate a reference number that is not already present in this database, to do this i know i am going to need to search the database to check whether that number is already present or not. But I am unsure of the best way to go about this. I am using an access database and vb.net. Thanks.
Is This A Good Question/Topic? 0
  • +

Replies To: Vb.net searching a database to see if value is already present

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,913
  • Joined: 25-September 09

Re: Vb.net searching a database to see if value is already present

Posted 08 February 2012 - 09:21 AM

Use a SELECT statement to Query the database for the value you want, Store it to a variable and manipulate your variable for when you're ready to save it back to the database.

Your post gives us no point of reference as to your abilities but since you stated you're in the process of using a database, we have to think you at least know how to SELECT data from it. Otherwise, post your code and show us where you're stuck.
OleDB Basics in VisualBasic.Net
Was This Post Helpful? 1
  • +
  • -

#3 Jerrrum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 08-February 12

Re: Vb.net searching a database to see if value is already present

Posted 08 February 2012 - 09:44 AM

My abilities aren't great as i have been programming for about half a year.
I haven't currently got any code for this section of my program as i am not sure what to do. I have an idea but cant get it down in code.

I have got the text box containing the desired reference number and a database that contains records also with reference numbers.

I want to do some sort of SELECT or LOOP to find out whether that reference is already present in the database or not as i want all of the references to be unique.

The thing i'm struggling with is getting my head around how to search the database to see if that reference number already exists or not.

Thanks for the interest.
Was This Post Helpful? 0
  • +
  • -

#4 Jerrrum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 08-February 12

Re: Vb.net searching a database to see if value is already present

Posted 08 February 2012 - 09:44 AM

My abilities aren't great as i have been programming for about half a year.
I haven't currently got any code for this section of my program as i am not sure what to do. I have an idea but cant get it down in code.

I have got the text box containing the desired reference number and a database that contains records also with reference numbers.

I want to do some sort of SELECT or LOOP to find out whether that reference is already present in the database or not as i want all of the references to be unique.

The thing i'm struggling with is getting my head around how to search the database to see if that reference number already exists or not.

Thanks for the interest.
Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,913
  • Joined: 25-September 09

Re: Vb.net searching a database to see if value is already present

Posted 08 February 2012 - 09:52 AM

Have you clicked on the link to the tutorial I provided? I recommend at least working through it to give you a better understanding of the basics. While it won't completely cover your question it will give you the ability to provide an attempt and give us something to work off of. This site isn't one that just hands out solutions, we take pride in helping you learn and not just become a copy-paste coder. There are a lot of volunteer individuals on this site that can help you but we need to see an effort on your part.
Was This Post Helpful? 0
  • +
  • -

#6 Jerrrum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 08-February 12

Re: Vb.net searching a database to see if value is already present

Posted 08 February 2012 - 10:11 AM

I will give it a go and see what i come out with, thanks.
Was This Post Helpful? 0
  • +
  • -

#7 Jerrrum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 08-February 12

Re: Vb.net searching a database to see if value is already present

Posted 08 February 2012 - 10:51 AM

        
Dim i As Integer = 1000
Dim x As Integer = 0

        Do Until x = MaxRows

            If txtCustRefNum.Text = ds.Tables("CustomerDetails").Rows(x).Item("CustomerReference") Then
                txtCustRefNum.Text = (i + 1)

            ElseIf txtCustRefNum.Text Is Not ds.Tables("CustomerDetails").Rows(x).Item("CustomerReference") Then
                x = x + 1
            End If
            
        Loop


I have come up with this so far. Does it make any sort of sense or do you want me to explain what i am doing in each part of the code?
Was This Post Helpful? 0
  • +
  • -

#8 Jerrrum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 08-February 12

Re: Vb.net searching a database to see if value is already present

Posted 08 February 2012 - 12:10 PM

        MaxRows = ds.Tables("CustomerDetails").Rows.Count

        Do Until x = MaxRows

            If txtCustRefNum.Text = ds.Tables("CustomerDetails").Rows(x).Item("CustomerReference") Then
                txtCustRefNum.Text = ("CR" & i + 1)
                x = 0
            ElseIf txtCustRefNum.Text = ds.Tables("CustomerDetails").Rows(x).Item("CustomerReference") = False Then
                x = x + 1
            End If
            
        Loop


This code works for the first two entries in the database, when it gets to the third one it just freezes the whole program. Any idea why this may happen?
Was This Post Helpful? 0
  • +
  • -

#9 Jerrrum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 08-February 12

Re: Vb.net searching a database to see if value is already present

Posted 08 February 2012 - 12:46 PM

YES! Have finally figured it out. Just had to take the i+1 out of the bracket.
i = i + 1
                TextBox1.Text = (i)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1