Problem with an ordinal number when creating a new entry.

Masta

New Member
Joined
Feb 22, 2022
Messages
33
Office Version
  1. 2021
  2. 2019
  3. 2016
Platform
  1. Windows
I have one problem and i don't know how to work around that, so i need help.

Private Sub CommandButton42_Click()

TextBox47.Value = TextBox47.Value & TextBox6.Value

TextBox6.Value = ""
TextBox2.Value = ""
TextBox3.Value = ""
TextBox4.Value = ""

Sheets("Baza").Range("A1").Value = Sheets("Baza").Range("A1").Value + 1 'Generisanje rednog broja 'Generate a new ordinal number
TextBox46.Text = Sheets("Baza").Range("A1") 'Generisanje rednog broja 'Generate a new ordinal number

TextBox2.BackColor = RGB(240, 240, 240) 'Bela
TextBox3.BackColor = RGB(240, 240, 240) 'Bela

End Sub

The problem is when I don't have data in TextBox6 when I click to execute the command a new number is generated and I don't want that to happen when there is no data in TexBox6, I want the number to be generated only when data is present in TexBox6 and needs to be copied to TextBox47. Otherwise, the ordinal number is generated in TextBox46 only when there is a condition for it. Thanks for the help if you know how to help me.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Please post code within code tags (vba button on posting toolbar) and use proper indentation for more than a few lines. Maybe
VBA Code:
Private Sub CommandButton42_Click()

If Not textbox6 & vbnullstring = "" Then
   TextBox47.Value = TextBox47.Value & TextBox6.Value
   TextBox6.Value = ""
   TextBox2.Value = ""
   TextBox3.Value = ""
   TextBox4.Value = ""

   Sheets("Baza").Range("A1").Value = Sheets("Baza").Range("A1").Value + 1 'Generisanje rednog broja 'Generate a new ordinal number
   TextBox46.Text = Sheets("Baza").Range("A1") 'Generisanje rednog broja 'Generate a new ordinal number

   TextBox2.BackColor = RGB(240, 240, 240) 'Bela
   TextBox3.BackColor = RGB(240, 240, 240) 'Bela
End If

End Sub
I didn't study the rest of your code and I don't know what this means.
Otherwise, the ordinal number is generated in TextBox46 only when there is a condition for it.
VbNullString works in Access - I don't see why it wouldn't work here. I don't think you can use IsEmpty on a userform control so I suggested the above.
 
Upvote 0
Solution
Please post code within code tags (vba button on posting toolbar) and use proper indentation for more than a few lines. Maybe
VBA Code:
Private Sub CommandButton42_Click()

If Not textbox6 & vbnullstring = "" Then
   TextBox47.Value = TextBox47.Value & TextBox6.Value
   TextBox6.Value = ""
   TextBox2.Value = ""
   TextBox3.Value = ""
   TextBox4.Value = ""

   Sheets("Baza").Range("A1").Value = Sheets("Baza").Range("A1").Value + 1 'Generisanje rednog broja 'Generate a new ordinal number
   TextBox46.Text = Sheets("Baza").Range("A1") 'Generisanje rednog broja 'Generate a new ordinal number

   TextBox2.BackColor = RGB(240, 240, 240) 'Bela
   TextBox3.BackColor = RGB(240, 240, 240) 'Bela
End If

End Sub
I didn't study the rest of your code and I don't know what this means.

VbNullString works in Access - I don't see why it wouldn't work here. I don't think you can use IsEmpty on a userform control so I suggested the above.
Thank you for the fast and correct response! :love:(y)
 
Upvote 0
Please post code within code tags (vba button on posting toolbar) and use proper indentation for more than a few lines. Maybe
VBA Code:
Private Sub CommandButton42_Click()

If Not textbox6 & vbnullstring = "" Then
   TextBox47.Value = TextBox47.Value & TextBox6.Value
   TextBox6.Value = ""
   TextBox2.Value = ""
   TextBox3.Value = ""
   TextBox4.Value = ""

   Sheets("Baza").Range("A1").Value = Sheets("Baza").Range("A1").Value + 1 'Generisanje rednog broja 'Generate a new ordinal number
   TextBox46.Text = Sheets("Baza").Range("A1") 'Generisanje rednog broja 'Generate a new ordinal number

   TextBox2.BackColor = RGB(240, 240, 240) 'Bela
   TextBox3.BackColor = RGB(240, 240, 240) 'Bela
End If

End Sub
I didn't study the rest of your code and I don't know what this means.

VbNullString works in Access - I don't see why it wouldn't work here. I don't think you can use IsEmpty on a userform control so I suggested the above.
In place of this...

If Not textbox6 & vbnullstring = "" Then

wouldn't this work as well...

If Len(textbox6) Then
 
Upvote 0
I think you mean IF Len(textbox6) = 0 ? Then yes.
Len(textbox6) will return 0 if it contains no characters and 0 is the integer for False so you'd have to be careful of whether or not you use If Not vs If.
You probably know Access' Nz function, which I prefer, but it isn't an option in Excel, unfortunately.
As for vbNullString, it is faster to assign and uses less memory (not that it should be an issue in most cases these days). It's a null pointer of type string (but doesn't actually point to a string). "" uses 6 bytes whereas vbNullString uses none but AFAIK, that only matters if you're assigning a variable a value, and even then, how bad is that anymore? So my usual options in Access vba, where I've coded most often up to now is:
If Nz(textbox6,"") = "" Then (or substitute 0 for "")
If Nz(textbox6, vbNullString) = vbNullString
If Len(textbox6) = 0 Then
If textbox6 & vbNullString = "" Then
However, if reference (control) has no value,
reference = vbNullString will return False (because it's Null) but
reference & vbNullString = vbNullString will return True
Something tells me that much of that doesn't matter in Excel.
 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,506
Members
449,089
Latest member
RandomExceller01

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