I'm trying to insert a Visual Basic Code Snippet into my latex document. I've got listings package intalled and it works for C++ code. But when I try to use the following command it shows the error (Package Listings Error: File B(.tex) not found).

\small
\singlespace
\lstinputlisting[frame=single,title={VarGlob.bas},language=[Visual]Basic)]{codigo/VarGlob.vb}
\normalsize
\onehalfspace


Can someone help me figuring this out please?

Thank you.

Generally, the way LaTeX handles optional arguments, is that when a [ is seen after a macro name, it considers everything from that [ to the first ] it sees in the same brace level as the optional argument. Hence, when you have language=[Visual]Basic, LaTeX sees the ] after Visual as the end of the optional argument. \lstinputlisting has one mandatory argument, so LaTeX then grabs the next token it finds, the B in Basic and that becomes the mandatory argument.

This explains the error you get -- B is read as the filename, and listings seems to have .tex as a default extension (based on the error message).

Hence, you have to do language={[Visual]Basic}, with an extra set of braces to not have that ] interpreted as the end of the optional argument.

Further, instead of the \small\singlespace/\normalsize\onehalfspace, I would add the \small\singlespace to the basicstyle of the listing. Note that if you have a \lstset in the preamble to set the general style, I would add it there instead.

\documentclass{article}
\usepackage{listings}
\usepackage{setspace}
\usepackage{lipsum} % just for dummy text, no need to include it in your actual document

\begin{document}
\onehalfspace
\lipsum[1]
\lstinputlisting[
basicstyle=\small\singlespace,
frame=single,
title={VarGlob.bas},
language={[Visual]Basic}
]{codigo/VarGlob.vb}
\lipsum[2]
\end{document}