Excel Inputbox displaying 0 instead of empty string - excel

I am working on an excel vba project and have run into an odd issue. I am working inside of a button click sub and want to display the current note as the default while they update the note. Unfortunately, all empty cells show up with a default value of "0". The msgbox just before is for debugging purposes and it shows "" (nothing). Any ideas?
MsgBox Sheet4.Range("E" & stallToAdd + 1)
inputFromUser = Application.InputBox("Update note: ", "Update Note", Sheet4.Range("E" & stallToAdd + 1))
Edit: inputFromUser is a variant, stallToAdd is an integer.

Just convert it to a string like this:
inputFromUser = Application.InputBox("Update note: ", "Update Note", _
CStr(Sheet4.Range("E" & stallToAdd + 1)))

Related

Run-time error 13: Listbox with checkboxes to excel cells

I click the button and I can fill the rows with all the values from the textboxes, but the listbox is not working, I can see the items but when I select them and click the commandbutton2 they don't transfer .
Keeps showing the Run-time Error 13 Type mismatch.
To add items to my listbox I used the RowSource in the ListBox Properties =Sheetname!G12:G24 and when I open the Form it everything there.
Private Function selectneg() As Integer
Dim i As Integer
For i = LBound(ListBox1.List) To UBound(ListBox1.List)
If ListBox1.Selected(i) Then
selectneg = selectneg & ListBox1.List(i) & " , "
End If
Next i
If Len(selectneg) > 0 Then
selectneg = Left(selectneg, Len(selectneg) - 1) 'here is where the error appear
End If
When you use selectneg = selectneg & ListBox1.List(i) & " , " you are trying to put an string in an integer.
If you need a string use a string, you can't use a different type as string

Userform button not working when typing in a textbox vba

I have a userform where you have to enter data into textboxes and then press a button to put the data into the sheet. When I'm typing into the last textbox and I'm done, I click the button and it doesn't respond. Also, I have noticed that my cursor keeps blinking inside the last textbox so I guess there's a problem there (while focused not able to click a button)?
In total I have 4 textboxes, 3 of them use data validation after their value has been updated. The last one does not have data validation.
The weird thing is that, next to the "next button", I have a button to clear the fields and that one works just fine. Below an image from my userform with a little bit of explanation because it's in another language (Dutch). Can anyone help me? Thanks!
The code used for the "next" button is:
Note: the data gets validated not only when they updated the value of the textbox, but also an extra time when they click the next button.
Private Sub AddNextBtn_Click()
AddValueMod.AddDisplayOverview
End Sub
Sub AddDisplayOverview() 'This sub is in the "AddValueMod" module
'Check if information is valid via a function
If AddInformationValid("AccountSelector", True) And AddInformationValid("Date", True) And AddInformationValid("Amount", True) And AddInformationValid("Description", True) Then
'If valid, retrieve entered values
Dim account, dDate, amount, description As String
account = main.AddAccountSelector.Value
dDate = main.AddDateInput.Value
amount = main.AddValue.Value
description = main.AddDescription.Value
'Ask for sheet-writing-confirmation
overview = MsgBox("Kloppen volgende gegevens (kijk goed na!)?" & vbCrLf & vbCrLf & "Rekening: " & account & vbCrLf & "Datum: " & dDate & vbCrLf & "Bedrag: " & amount & vbCrLf & "Beschrijving: " & description & vbCrLf & "Vermeerdering/vermindering: Waarde wordt vermeerderd", vbYesNo + vbQuestion, "Kloppen volgende gegevens?")
If overview = vbYes Then
'Write data to sheet
AddValueMod.AddEnterDataIntoSheet
End If
End If
End Sub
And for the "clear fields" button:
Private Sub AddClearFieldsBtn_Click()
AddValueMod.AddClearFields (True)
End Sub
Sub AddClearFields(askForConfirmation As Boolean) 'This sub is in the "AddValueMod" module
grey = RGB(128, 128, 128)
'If askForConfirmation = True, ask for confirmation before clearing fields
If askForConfirmation = True Then
confirmationMessage = MsgBox("Bent u zeker dat u de velden wilt leegmaken?" + vbCrLf + "U zal terug opnieuw moeten beginnen.", vbYesNo + vbQuestion, "Velden leegmaken?")
If confirmationMessage = vbYes Then
'Clear fields
main.AddAccountSelector.Value = ""
main.AddDateInput.Value = ""
main.AddValue.Value = ""
main.AddDescription.Value = ""
End If
ElseIf askForConfirmation = False Then
'Clear fields
main.AddAccountSelector.Value = ""
main.AddDateInput.Value = ""
main.AddValue.Value = ""
main.AddDescription.Value = ""
End If
'Reset the textboxes' borders (they change if an input error occurred)
main.AddAccountSelectorError.Visible = False
main.AddAccountSelector.BorderStyle = fmBorderStyleSingle
main.AddAccountSelector.BorderColor = grey
main.AddDateInputError.Visible = False
main.AddDateInput.BorderStyle = fmBorderStyleSingle
main.AddDateInput.BorderColor = grey
main.AddValueError.Visible = False
main.AddValue.BorderStyle = fmBorderStyleSingle
main.AddValue.BorderColor = grey
main.AddDescriptionError.Visible = False
main.AddDescription.BorderStyle = fmBorderStyleSingle
main.AddDescription.BorderColor = grey
End Sub
P.S.: I've already tried a possible solution with IsCancel = True/False that I found online in this article. It's possible it didn't work because the article is not quite related to my problem but I felt like I should mention it :).
You did not provide all relevant code. AddInformationValid() and AddEnterDataIntoSheet are missing.
However, if the AddInformationValid() returns False on any item, the behaviour is just as you describe. In AddDisplayOverview() there will be no error message, the AddEnterDataIntoSheet sub will be bypassed and on return from the button handler the cursor remains flashing in the last entry field.
You need to verify and correct the AddInformationValid() Function.
I also strongly recommend to show an error message if the data validation fails.

Exporting MS Access 2016 form with empty fields to an Excel spreadsheet using SQL

I am exporting MS Access 2016 form data to an Excel spreadsheet.
Dim ctrlForm As Control
Dim sqlSelect As String
sqlSelect = "Select "
For Each ctrlForm In Forms![Student Listing].Form.Controls
If TypeOf ctrlForm Is TextBox Then
If ctrlForm .ColumnHidden = False Then
' this prints 109, which is text box - perfect
Debug.Print "Control Type: " & ctrlForm .ControlType
sqlSelect = sqlSelect & ctrlForm .Name & ","
End If 'end if for hidden
End If 'end if for TypeOf
Next ctrlForm
sqlSelect = Left(sqlSelect, Len(sqlSelect) - 1)
sqlSelect = sqlSelect & " From " & Forms![Student Listing].Form.RecordSource
'prints the SQL statement - perfect too, all the view-able fields
Debug.Print "SQL Prompt: " & sqlSelect
Dim rs As Recordset
'errors here with: Run-time error '3061' Too few parameters.
'Expected 1(or some other number) - even if there are 30 entries in the sqlSelect variable.
Set rs = CurrentDb.OpenRecordset(sqlSelect)
If I hide all fields which may contain 'no data - or empty', it works. The biggest issue is the middle initial, but I suspect looking for null/empty is the key. How do I go about doing that and keep the empty field(column) for the exported Excel file?
"Too few parameters" means you have a field name specified, with no value (even an empty one - in this case, not enough commas in the data part of the string)
option 1: your only fault may be the space inbetween "ctrlForm .Name"
if that doesn't fix it, try:
a test if the control text is empty - when it is, write out an empty string (or an error message) and a comma, not skip the field.
so after the debug:
if .Name <> "" then
sqlSelect = sqlSelect & ctrlForm .Name & ","
else
sqlSelect = sqlSelect & ","
end if
Thank you for all of your help. I found the issue. The issue turned out to be date fields. If I don't 'export' a date field, everything works. If a date field is exported - the error message I described above. Apparently, this is actually a known thing. The work around is to create a secondary 'text' field that contains the text of the date field. Export that field instead. No loss of functionality, but the export works fine.

Place data in multiple cells with command button

I connected two weighing scales to my PC and used a VBA tutorial and the XMComm to create Excel userform that collects the weight data from a scale and places it in a cell.
I can retrieve the weight from each scale with separate command buttons. I would like to combine both scales into one command button.
I've tried by putting ActiveCell.Offset(0,1).Select between the two Userforms in the command button. However, when I use this command button, the Active Cell shifts right immediately and only one of the two weight values is placed.
I use ActiveCell to place this information in any cells.
I think it may be an issue with the individual userforms themselves. When I try to place a single weight from a command button tied to just one userform, the weight is sometimes not placed.
Here is the userform code:
Private Sub XMCommCRC1_OnComm()
Static sInput As String
Dim sTerminator As String
Dim Buffer As Variant
' Branch according to the CommEvent property
Select Case XMCommCRC1.CommEvent
Case XMCOMM_EV_RECEIVE
Buffer = XMCommCRC1.InputData ' Use Input property for MSComm
sInput = sInput & Buffer
If Worksheets("Settings").Range("Terminator") = "CR/LF" Then
sTerminator = vbCrLf
Else
sTerminaotr = vbCr
End If
If Right$(sInput, Len(sTerminator)) = sTerminator Then
XMCommCRC1.PortOpen = False
sInput = Left$(sInput, Len(sInput) - Len(sTerminator))
Select Case Left$(sInput, 2)
Case "ST", "S "
ActiveCell.Value = CDbl(Mid$(sInput, 7, 8))
ActiveCell.Activate
Case "US", "SD"
MsgBox "The balance is unstable."
Case "OL", "SI"
MsgBox "The balance is showing an eror value."
End Select
sInput = ""
End If
End Select
End Sub
Public Sub RequestBalanceData()
With Worksheets("Settings")
' Configure and open the COM port
If Not XMCommCRC1.PortOpen Then
XMCommCRC1.RThreshold = 1
XMCommCRC1.RTSEnable = True
XMCommCRC1.CommPort = .Range("COM_Port")
XMCommCRC1.Settings = .Range("Baud_Rate") & "," & _
.Range("Parity") & "," & _
.Range("Data_Bits") & "," & _
.Range("Stop_Bits")
XMCommCRC1.PortOpen = True
End If
' Send balance's "SI" (Send Immediate) command
' to request weighing data immediately
If .Range("Terminator") = "CR/LF" Then
XMCommCRC1.Output = "R" & vbCrLf
Else
XMCommCRC1.Output = "R" & vbCr
End If
End With
End Sub
I am using Excel 2007.
The VBA tutorial - http://www.msclims.com/lims/diybalance.html
The link to XMCOMM - http://www.hardandsoftware.net/xmcomm.htm

Display text link to open specific PDF page from Excel

I found a VBA code online that opens up an internal (shared drive) PDF document page in IE (e.g. goes to page 8 of PDF file). I would like to display text in the cell for a user to click (e.g. "Click here to view").
Problem: The cell currently displays '0' and I have to go to the function bar and hit [Enter] to execute.
Excel Version: 2003
Function call:
=GoToPDFpage("S:\...x_2011.pdf",8)
VBA Code:
Function GoToPDFpage(Fname As String, pg As Integer)
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Navigate Fname & "#page=" & pg
.Visible = True
End With
End Function
:EDIT:
I was able to display text, but it's still not a link like I wanted.
="Click to view" & GoToPDFpage("S:\...x_2011.pdf",8)
Thank you for your help.
If you dont have a high complex workbook/worksheet you could try the following:
Turn the "Click to view" cell into a Hyperlink with following characteristics.
Make it point to itself
The text inside the cell must always be the string Page= plus the number that you what the pdf to open in. Eg.: Page=8
Then go to the workseet module and paste the following code:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
If Left(ActiveCell.Value, 4) = "Page" Then
GoToPDFpage Range("A1").Value, Mid(ActiveCell.Value, 6)
'This code asumes that the file address is writen in the cell A1
End If
'
End Sub
'
The above written code will trigger every time you run a hyperlink in the worksheet.
As the hyperlink always point to itself, the "Activecell.Value" will always have the page number that you want to open.
I'm assuming that you can put the file address in the cell A1. You could modify this portion to point to any other cell. (including: The cell to the right of the current hyperlink, etc).
This might not be the best option, but if you only need a quick feature in a couple of cells, it might be enough.
Hope it helps !
EDIT:
To make each HLink reference to itself, you can select all the cells where you have the links and then run this procedure:
Sub RefHLink()
Dim xCell As Range
For Each xCell In Selection
ActiveSheet.Hyperlinks.Add Anchor:=xCell, Address:="", SubAddress:= _
xCell.Address, ScreenTip:="Click Here", TextToDisplay:="Page="
Next xCell
End Sub
how about letting excel write a batch file then running it?
*edit paths to pdf and AcroRd32.exe
Sub batfile()
Dim retVal
filePath = "path\pdf.bat"
pg = 2
Open filePath For Output As #1
Print #1, "Start /Max /w " & Chr(34) & "Current E-book" & Chr(34) & " " & Chr(34) & "C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe" & Chr(34) & " /a " & Chr(34) & "page=" & pg & Chr(34) & " " & Chr(34) & "H:\Documents\RPG\Dragonlance\New folder\Sample File.pdf" & Chr(34) & ""
Close #1
retVal = Shell(strFilePath)
End Sub
Try Menu->Data->Data Validation. In the 2nd tab you can write your message.

Resources