Making Database Entries Unique?

mageeg

Board Regular
Joined
Jun 6, 2021
Messages
81
I have a userform in which the user enters data. This data is then sent to a database sheet.

This is done through the following code:

VBA Code:
Dim sh As Worksheet
    Dim iRow As Long
 
    Set sh = ThisWorkbook.Sheets("Database")
    
    
    If frmForm.txtRowNumber.Value = "" Then
    
        iRow = [Counta(Database!A:A)] + 1
    Else
    
        iRow = frmForm.txtRowNumber.Value
        
    End If
    
    
    With sh
    
    'adding each row to database
    
        .Cells(iRow, 1) = "=Row()-1" 'Dynamic Serial Number
        
        .Cells(iRow, 2) = frmForm.ModelNo.Value
        
        .Cells(iRow, 3) = frmForm.PartNo.Value
        
        .Cells(iRow, 4) = frmForm.WorksOrderNo.Value
        
        .Cells(iRow, 5) = frmForm.SerialNo.Value
        
        .Cells(iRow, 6) = frmForm.MaterialNo.Value
        
        .Cells(iRow, 7) = frmForm.SerialNumber.Value
        
        .Cells(iRow, 8) = frmForm.txtType.Value
        
        .Cells(iRow, 9) = frmForm.txtSize.Value

How would i make particular entries unique?

For example maybe i would like the ModelNo to be unique. So only one entry in the database should have a particular Model Number.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
One way is to use the COUNTIF function (Application.WorksheetFunction.CountIf...) to count how many time that value already exists in a particular column.
If it is more than 0, then it already exists, and you can alert the user and not add the record.

If you have a few fields you want to check like this, it may be good to create a separate User Defined Function called something like "ValidateEntries" that checks each one and returns a True or False, depending on whether or not the entry meets your requirements and the record can be added.
 
Upvote 0
One way is to use the COUNTIF function (Application.WorksheetFunction.CountIf...) to count how many time that value already exists in a particular column.
If it is more than 0, then it already exists, and you can alert the user and not add the record.

If you have a few fields you want to check like this, it may be good to create a separate User Defined Function called something like "ValidateEntries" that checks each one and returns a True or False, depending on whether or not the entry meets your requirements and the record can be added.

That sounds like what i would be wanting to do.

I am quite new to this, but have been progressing well. Would you be able to help me adjust my code to use the COUNTIF function?
 
Upvote 0
Before your "With sh" line, you would have a block of code like this:
VBA Code:
    If Application.WorksheetFunction.CountIf(Columns("B:B"), frmForm.ModelNo.Value) > 0 Then
        MsgBox frmForm.ModelNo.Value & " already exists in column B.", vbOKOnly, "PLEASE TRY AGAIN!"
        Exit Sub
    End If
 
Upvote 0
Solution
Thanks for getting back to me!

Hmm i can't seem to get this to work. Still allows me to add duplicate values for model number
 
Upvote 0
Try adding the sheet qualifier to your range, i.e. change this:
VBA Code:
Columns("B:B")
to this:
VBA Code:
sh.Columns("B:B")

If that does not work, please post your amended code for us to see, to make sure that you have implemented it correctly.
 
Upvote 0
Try adding the sheet qualifier to your range, i.e. change this:
VBA Code:
Columns("B:B")
to this:
VBA Code:
sh.Columns("B:B")

If that does not work, please post your amended code for us to see, to make sure that you have implemented it correctly.
Thanks!

That little change worked :)
 
Upvote 0
Before your "With sh" line, you would have a block of code like this:
VBA Code:
    If Application.WorksheetFunction.CountIf(Columns("B:B"), frmForm.ModelNo.Value) > 0 Then
        MsgBox frmForm.ModelNo.Value & " already exists in column B.", vbOKOnly, "PLEASE TRY AGAIN!"
        Exit Sub
    End If

Whilst this worked great, would it be possible to enhance this further and say,

The PartNo and SerialNo can be used multiple times but is only unique together?

e.g. If SerialNo was 1710, it can be used multiple times, but only once with the PartNo of 2805SC
 
Upvote 0
Whilst this worked great, would it be possible to enhance this further and say,

The PartNo and SerialNo can be used multiple times but is only unique together?

e.g. If SerialNo was 1710, it can be used multiple times, but only once with the PartNo of 2805SC
You can use the COUNTIFS function, which allows you to check multiple fields at once.
So you can check your desired combination of fields, to see if that is unique, in the same manner we did the first one.
See here for the layout of the COUNTIFS function: MS Excel: How to use the COUNTIFS Function (WS)

It will be a good exercise to see how well you understand the first part. See how you do, and if you run into issues, post back here, along with the code you tried.
 
Upvote 0
You can use the COUNTIFS function, which allows you to check multiple fields at once.
So you can check your desired combination of fields, to see if that is unique, in the same manner we did the first one.
See here for the layout of the COUNTIFS function: MS Excel: How to use the COUNTIFS Function (WS)

It will be a good exercise to see how well you understand the first part. See how you do, and if you run into issues, post back here, along with the code you tried.

hmm i just had a try with the below code. however it is throwing the already exists error everytime I save data, after testing i believe this is because it is seeing that one of them is not unique and is throwing the error. Where as for that error to appear i want them to both have been used TOGETHER before.

Code:
If Application.WorksheetFunction.CountIf(sh.Columns("C:C"), frmForm.PartNo.Value) + Application.WorksheetFunction.CountIf(sh.Columns("E:E"), frmForm.SerialNo.Value) > 0 Then
        MsgBox frmForm.PartNo.Value & frmForm.SerialNo.Value & " This combination of PartNo and SerialNo already exists", vbOKOnly, "PLEASE TRY AGAIN!"
        Exit Sub
    End If
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,942
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top